> However this doesn't produce good results if your JSON contains a key you 
> haven't listed in fields –
> so you'd probably have to write an actual function to do the mapping.

(def my-ns *ns*) ; bind at compile time
;; or use a constant string for the ns if you want
(defn fields [k] (keyword (str my-ns) k))

(fields “name”)
;;=> :chtst.core/name

Sean Corfield -- (970) FOR-SEAN -- (904) 302-SEAN
An Architect's View -- http://corfield.org/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood

From: Peter Hull
Sent: Monday, August 7, 2017 1:04 AM
To: Clojure
Subject: Re: Converting json to work with clojure.spec


On Sunday, 4 December 2016 15:11:55 UTC, Jonathon McKitrick wrote:
That being said, I see the benefits in moving to namespace qualified keys. 
Currently, I'm returning structures directly in Compojure handlers, and the 
JSON conversion is implicitly handled. I checked Cheshire and didn't 
immediately see a way to generate namespaced keys. What's the best way to do 
this?
parse-string takes a second argument that converts from a JSON string key to 
whatever you want for your clojure key - see 3rd example in 
https://github.com/dakrone/cheshire#decoding
So you could have something like:
 
(ns chtst.core
  (:require [cheshire.core :refer [parse-string]]
            [clojure.pprint :refer [pprint]])
  (:gen-class))

(def fields { "name" ::name, "age" ::age})

(defn -main
  [& args]
  (let [example (parse-string "{\"name\":\"fred\", \"age\":29}" fields)]
    (pprint example)))



However this doesn't produce good results if your JSON contains a key you 
haven't listed in fields - so you'd probably have to write an actual function 
to do the mapping.

Does that help?
Pete
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to