How to transform object keys and values in Clojure

Viewed 512

I'm using Datomic, although it doesn't particularly matter for this question. But it typically returns namespaced keys (and enum values are returned as namespaces keywords also). I want to translate the potentially nested structure to strip the namespaces from the keys and from values (and also string-ify the values of enums). I'm doing this because I'll return the result in a JSON REST API and the namespacing doesn't make much sense in that context. Here's a simple example structure:

{ 
    :person/name "Kevin"
    :person/age 99
    :person/gender :gender/M
    :person/address {
        :address/state :state/NY
        :address/city "New York"
        :address/zip "99999"
    }
}

And I'm hoping to translate to:

{ 
    :name "Kevin"
    :age 99
    :gender "M"
    :address {
        :state "NY"
        :city "New York"
        :zip "99999"
    }
}

One thing I know I can do is use (postwalk-replace {:person/name :name :person/age :age :person/gender :gender :person/address :address :address/city :city :address/state :state :address/zip :zip} the-entity) and that covers the keys, but not the values.

What other options do I have?

2 Answers
Related