Thursday, 15 August 2013

Clojure - Unable to parse json in response's body -



Clojure - Unable to parse json in response's body -

i'm having issue parsing json came in response's body... can value body. however, when i'm trying value json in body, i'm unable to.

this function i'm using:

(ns cs4261-server.handler (:use compojure.core) (:require [compojure.handler :as handler] [compojure.route :as route] [clojure.data.json :as json] [ring.util.response :as resp] [org.httpkit.client :as http] [clj-http.client :as client] [org.httpkit.server :as hs] [ring.middleware.json :refer [wrap-json-params]] [ring.middleware.jsonp :refer [wrap-json-with-padding]])) (defn get-recipes [param] (let [req (str "http://api.yummly.com/v1/api/recipes?_app_id=78dccce1&_app_key=87460fca330c28f52a9603ababd5a54f&allowedcourse[]=course^course-dinner&maxtotaltimeinseconds=3600&" param)] (let [resp (client/get req)] (let [body (json/read-str (get resp :body))] (get body :matches))))) (defroutes app-routes (get "/" [] (json/write-str "this start screen appease myself."})) (get "/somerecipe" {params :query-string} (get-recipes params)) (get "/recipes" [] (json/write-str { :message "this other different response"})) (route/resources "/") (route/not-found "this isn't page you're looking for.."))

and when run (keys body): attributiontotalmatchcountfacetcountsmatchescriteria

(vals body) gets me:

{"html" "recipe search powered yummly", "url" "http://www.yummly.com/recipes/", "text" "recipe search powered yummly", "logo" "http://static.yummly.com/api-logo.png"}73458{}[{"ingredients" ["fresh basil" "green onions" "mayo" "grated parmesan cheese" "salt" "ground black pepper" "cooked chicken"], "totaltimeinseconds" 2700.0, "sourcedisplayname" "kalyn's kitchen", "flavors" nil, "attributes" {"course" ["salads"]}, "imageurlsbysize" {"90" "http://lh4.ggpht.com/xggegxigwvnoymmiirn36buboh9rcy40mxh710hi2imcq7la3ormr_9cxzpk-r_5rvoler9h5k8fmhv0gqitjyg=s90-c"}, "smallimageurls" ["http://lh4.ggpht.com/e7-jnivwaicn2_d5-ht5jr52w-zdtb6duynp2qnwdi1y9kvruotx-2y0bkekwktrmtxghh2fjgv7_wchojr_=s90"], "recipename" "chicken salad basil , parmesan", "id" "chicken-salad-with-basil-and-parmesan-309576", "rating" 5} {"ingredients" ["dried breadcrumbs" "grated parmesan cheese" "eggs" "boneless skinless chicken breasts" "italian seasoning"], "totaltimeinseconds" 2700.0, "sourcedisplayname" "allrecipes", "flavors" {"sour" 0.16666666666666666, "salty" 0.8333333333333334, "sweet" 0.16666666666666666, "bitter" 0.5, "meaty" 0.6666666666666666}, "attributes" {}, "imageurlsbysize" {"90" "http://lh4.ggpht.com/wsztxhlua4qpwg2ij1lzimfxl2lcew05w1vbbv8niphz7bdyjmiaemtsycq3yfsd4imgykvvpfuuwxokchi4rgg=s90-c"}, "smallimageurls" ["http://lh5.ggpht.com/xkuecmtw7w9-8go_7sistol4wefo7xxendjf9sowj6q0khm8s3smhrzeuiprkkslbyp4cohtuanfmj-_ralz2w=s90"], "recipename" "simply parmesan chicken", "id" "simply-parmesan-chicken-allrecipes", "rating" 5} {"ingredients" ["safflower oil" "onion" "kosher salt" "freshly ground pepper" "cooked chicken" "sweet paprika"], "totaltimeinseconds" 3000.0, "sourcedisplayname" "big girls little kitchen", "flavors" {"sour" 0.3333333333333333, "salty" 0.16666666666666666, "sweet" 0.6666666666666666, "bitter" 0.16666666666666666, "piquant" 0.3333333333333333, "meaty" 0.16666666666666666}, "attributes" {}, "imageurlsbysize" {"90" "http://lh3.ggpht.com/uxaxd4wzogssrbrh1mgq8biidjn-cq95khtroose8vql9iscixr1vbxie2ycsrzdiwvd-kf9iq7ypgtk_zmbf1o=s90-c"}, "smallimageurls" ["http://lh4.ggpht.com/cug6enz5zaoi-gbctkfz9a123pazek15qtk53phtvpnc1ph92o-ayqt4pdpgp_oycttlvgu4m8e-kv7daa7lcxs=s90"], "recipename" "chicken fritz", "id" "chicken-fritz-466963", "rating" 5} {"ingredients" ["fresh mozzarella" "fresh basil" "olive oil" "salt" "tomatoes" "balsamic vinegar" "cooked chicken"], "totaltimeinseconds" 900.0, "sourcedisplayname" "steamy kitchen", "flavors" {"sour" 0.3333333333333333, "salty" 1.0, "sweet" 0.5, "bitter" 0.8333333333333334, "meaty" 1.0}, "attributes" {}, "imageurlsbysize" {"90" "http://lh5.ggpht.com/mxmrfdlgi8sxjk0llp3g-y_fpjrx0ji3zx06jomf_yukluzddfnr-c7kkiwe10xp5pksrssjcvf7qphdapwt=s90-c"}, "smallimageurls" ["http://lh6.ggpht.com/yv6ibdkzqbl2ded-s6yru0qnxyum3_qwimlhkqilnhg14w7yskjdelus7ehvhq4qfw0qd1ipelmzjgxyol3bdw=s90"], "recipename" "chicken caprese", "id" "chicken-caprese-512479", "rating" 5} {"ingredients" ["coarse salt" "boneless chicken chest halves" "kalamata" "grape tomatoes" "olive oil" "capers" "freshly ground pepper"], "totaltimeinseconds" 1800.0, "sourcedisplayname" "martha stewart", "flavors" {"sour" 1.0, "salty" 0.3333333333333333, "sweet" 0.8333333333333334, "bitter" 0.6666666666666666, "meaty" 0.16666666666666666}, "attributes" {"cuisine" ["mediterranean"]}, "imageurlsbysize" {"90" "http://lh4.ggpht.com/hytmp4sginq_oxhez5d_wdjibzyfcx3fxdcw0pi_0g6ppc10rwvhcroffwo3qy2jofdarfyjd2djy1okmv8smre=s90-c"}, "smallimageurls" ["http://lh5.ggpht.com/2iliwjeertg18oblcm8hxqvcsme-rkazlrtp1lp_l9tzo7bwcsw83sljop5jxerrcyfdjqcw8nm6dwgpyruu-q=s90"], "recipename" "mediterranean chicken", "id" "mediterranean-chicken-martha-stewart", "rating" 5} {"ingredients" ["smoked paprika" "garlic salt" "meat" "boneless chicken skinless thigh" "extra-virgin olive oil"], "totaltimeinseconds" 2700.0, "sourcedisplayname" "the shiksa in kitchen", "flavors" nil, "attributes" {}, "imageurlsbysize" {"90" "http://lh5.ggpht.com/c1wyap36fwsxoaartbnsx_aggygeiw5kz1buokotyvbavcvr_m4h4tynw9j697_rn2jy537lisutzs7mwpogzg=s90-c"}, "smallimageurls" ["http://lh4.ggpht.com/b1ldeb7dsj8r45fyumzmxfdybjn_ebc4rt2rxibacamh8gpkvk97xzaimbedgskdbkkrhvlus6rpm6rkxjv_=s90"], "recipename" "smoked paprika chicken", "id" "smoked-paprika-chicken-the-shiksa-in-the-kitchen-200675", "rating" 5} {"ingredients" ["cabbage" "flour" "fresh tomatoes" "lemon wedge" "eggs" "panko breadcrumbs" "vegetable oil" "salt" "tonkatsu sauce" "chicken breasts"], "totaltimeinseconds" 3000.0, "sourcedisplayname" "food republic", "flavors" {"sour" 0.6666666666666666, "salty" 0.16666666666666666, "sweet" 0.16666666666666666, "bitter" 0.16666666666666666, "meaty" 0.8333333333333334}, "attributes" {}, "imageurlsbysize" {"90" "http://lh5.ggpht.com/xkpwqdnk3bhveizg0zg9fb12wvm_o7kxxlde1s_uuarrooifhvayziipmmdrh8tmggqtus_fb2utpzdq7v1bbg=s90-c"}, "smallimageurls" ["http://lh3.ggpht.com/fasjj35yjtuk1jztrlwegskqw5epun212j7u47vnhjic4c8uq6txgoqqkkip9qwvuhygbkgn4yqder36lzenxq=s90"], "recipename" "chicken katsu", "id" "chicken-katsu-561368", "rating" 4} {"ingredients" ["soy sauce" "pineapple juice" "garlic powder" "brown sugar" "boneless skinless chicken breasts" "bourbon whiskey" "ground black pepper"], "totaltimeinseconds" 1800.0, "sourcedisplayname" "allrecipes", "flavors" {"sour" 0.3333333333333333, "salty" 0.8333333333333334, "sweet" 0.8333333333333334, "bitter" 0.3333333333333333, "meaty" 0.16666666666666666}, "attributes" {}, "imageurlsbysize" {"90" "http://lh4.ggpht.com/d31ftoc_u4gvv2u3zmkv_k6d5grexji4izdczyri5q8mrvdzo9uvkvnihrzfmvg1ywgi4rlq93whcyxktwobofe=s90-c"}, "smallimageurls" ["http://lh3.ggpht.com/ccdq7v9da78xi_itjktekf0cxv4g-ddnvj6wqcjvforjv3sziv2g03r-99qgm8bll3udnpwyrsbzoocdgngeca=s90"], "recipename" "whiskey chicken", "id" "whiskey-chicken-allrecipes", "rating" 4} {"ingredients" ["fish sauce" "bibb lettuce" "fresh lime juice" "fresh cilantro" "water" "ground chicken" "yellow onion"], "totaltimeinseconds" 1800.0, "sourcedisplayname" "food republic", "flavors" {"sour" 0.3333333333333333, "salty" 0.6666666666666666, "sweet" 0.16666666666666666, "bitter" 0.3333333333333333, "meaty" 0.6666666666666666}, "attributes" {}, "imageurlsbysize" {"90" "http://lh5.ggpht.com/pqwnmmmyfyu3ux62kuy8ag8tbdqyg1ag5gw9gko_oxk1wtmjar-rmgibgshndi-ynsosvojwspuotgp3ukf6=s90-c"}, "smallimageurls" ["http://lh5.ggpht.com/ja3somamrrvduas3qpdvdxjtbpeljhuaxdzzs1jfbls0rnvebyvz7fscaaehcczrghzjbh6iuh7rfmv-9dwf2bc=s90"], "recipename" "easy chicken lettuce wraps", "id" "easy-chicken-lettuce-wraps-513151", "rating" 5} {"ingredients" ["baby spinach leaves" "shredded mozzarella cheese" "kosher salt" "white onion" "extra-virgin olive oil" "ground black pepper" "chicken breasts" "bow-tie pasta" "philadelphia cooking creme" "minced garlic"], "totaltimeinseconds" 3600.0, "sourcedisplayname" "picky palate", "flavors" {"sour" 0.16666666666666666, "salty" 0.16666666666666666, "sweet" 0.16666666666666666, "bitter" 0.16666666666666666, "meaty" 0.6666666666666666}, "attributes" {}, "imageurlsbysize" {"90" "http://lh5.ggpht.com/oelx_e9omzthj-vgpmxclq2u2shsmqzn3py0wmfsom1fma2dhu_hccj4x-ujm0cfuzvrtlqlg317jtl4islv9a=s90-c"}, "smallimageurls" ["http://lh5.ggpht.com/nqigfnirp-31xevu5v6pz9xgvho0xcd2pmyzsqq9x_-mmayqhbbmlqqj4vc3vfnbm61tbilmtg_8-sw23kqwpg=s90"], "recipename" "chicken florentine bowtie pasta", "id" "chicken-florentine-bowtie-pasta-334939", "rating" 5}]{"excludedingredients" nil, "allowedingredients" nil, "terms" nil}

so both keys , vals there, should able something, i'm not. ideas?

pass :key-fn keyword json/read-str in order keyword keys. , can query map :matches (get body :matches) or (:matches body)

here illustration repl session:

(require '[clojure.data.json :as json]) (def js (json/read-str "{\"a\": 7, \"b\": 13}" :key-fn keyword)) (:a js) ;; gives 7

or without key-fn:

(def js (json/read-str "{\"a\": 7, \"b\": 13}")) ;; keys strings now, have utilize (get map key) (get js "a") ;; gives 7

json clojure

No comments:

Post a Comment