haskell-json icon indicating copy to clipboard operation
haskell-json copied to clipboard

2 questions

Open danielbom opened this issue 4 years ago • 4 comments

  1. What happening if I use this input? { "a": 1, "a": 2 }

  2. How I can get a value from parsed JSON? Ex: jsonParsed = runParser $ jsonValue "{ "config": { "mode": "dev" } }" jsonGet jsonParsed "config.mode"

danielbom avatar Mar 07 '20 00:03 danielbom

to query a JsonValue, you can implement a jmespath in haskell 👍

dan323 avatar Apr 21 '20 18:04 dan323

A simple 'jsonValueByKey objectJson stringKey' it's enough to me.

danielbom avatar Apr 23 '20 01:04 danielbom

The json might not be a jsonObject, so we need something more involved than that. Nevertheless, at the end of the youtube video, if I remeber correctly, there are examples to query the json.

dan323 avatar Apr 23 '20 14:04 dan323

Seeing ramda js, and sanctuary, I believe which a function to retrieve data from Json with this definition, it's a good improvement:

jsonPath :: JsonValue -> [String] -> Maybe JsonValue
jsonPath (JsonObject []) _ = Nothing
jsonPath (JsonObject (x:xs)) keys@(k:ks) =
  if k == fst x
  then jsonPath (snd x) ks
  else jsonPath (JsonObject xs) keys
jsonPath val [] = Just val
jsonPath _ _ = Nothing

Where the array of strings ('[String]') is the a array of keys to the value

danielbom avatar Jul 09 '20 14:07 danielbom