I have a piece of JSON string, which I want to parse in Erlang. It looks like:
({ id1 : ["str1", "str2", "str3"], id2 : ["str4", "str5"]})
I looked at mochijson2, and a couple of other JSON parsers, but I really could not figure out how to do it. Any help greatly appreciated!
I once used the erlang-json-eep-parser, and tried it on your data.
7> json_eep:json_to_term("({ id1 : [\"str1\", \"str2\", \"str3\"], id2 : [\"str4\", \"str5\"]})").
** exception error: no match of right hand side value
{error,{1,json_lex2,{illegal,"("}},1}
in function json_eep:json_to_term/1
Right, it doesn't like the parentheses.
8> json_eep:json_to_term("{ id1 : [\"str1\", \"str2\", \"str3\"], id2 : [\"str4\", \"str5\"]}").
** exception error: no match of right hand side value
{error,{1,json_lex2,{illegal,"i"}},1}
in function json_eep:json_to_term/1
And it doesn't like the unquoted keys:
18> json_eep:json_to_term("{ \"id1\" : [\"str1\", \"str2\", \"str3\"], \"id2\" : [\"str4\", \"str5\"]}").
{[{<<"id1">>,[<<"str1">>,<<"str2">>,<<"str3">>]},
{<<"id2">>,[<<"str4">>,<<"str5">>]}]}
That looks better.
So it seems that your data is almost JSON, at least as far as this parser is concerned.
you can work on your JSON at the JSONLint validator: http://www.jsonlint.com/
Your input is not quite JSON -- the keys need to be quoted, like this:
{ "id1" : ["str1", "str2", "str3"], "id2" : ["str4", "str5"]}
A good Erlang library for manipulating JSON is jsx
Have you looked at http://www.json.org/ ?
or download "json4erlang" from here: json-and-json-rpc-for-erlang
Your JSON keys are not valid according to https://www.ietf.org/rfc/rfc4627.txt. Once you correct it, there are plenty of JSON libraries for Erlang, my favorite is JSX(https://github.com/talentdeficit/jsx/):
MyJSON = { "id1" : ["str1", "str2", "str3"], "id2" : ["str4", "str5"]},
jsx:decode(MyJSON, [return_maps]).
And it will return an Erlang map data structure that can be manipulated to your needs http://learnyousomeerlang.com/maps
Related
I am new to this topic, but I hope you can help me. I can't figure out a correct JSON expression for solving my problem.
Given JSON structure (coming from zigbee2mqtt):
{
"message" : "announce",
"meta" : {
"friendly_name" : "Lamp1"
},
"type" : "device_announced"
}
What I am trying:
if $.type == 'device_announced' then return the friendly_name
which in this case is
Lamp1
If I understand correctly, you are looking for an expression like this:
$[?(#.type=='device_announced')].meta.friendly_name
So, we are filtering the root collection on types equal to the search string, and then drill down to the friendly_name. You can test this online here.
Note: Some implementations require you to wrap your JSON in an array [ ] to allow this kind of filtering.
How to read values from nested JSON without using any library like GSON or org.JSON?
JSON is :
{data: { "EV_TOT_AMT" : "12" , "EV_CURR" : "INR", "T_BASKET" : [{"ORDER" : "abc", "BASE" : "xyx"},{"ORDER" : "def", "BASE" : "mno"}] } }
I want to read specific values as EV_TOT_AMT , EV_CURR , ORDER.
As far as I know, Java doesn't includes a JSON parser inside it's core classes... so if you don't want to use an external library, you'll need to build your own JSON parser.
Of course, you can just search the JSON string for your desired substrings and get the values moving into the string from the next ":" to the next "," (if the first character after the ":" is not an "["). But this isn't a good approach, unless your JSON input string is going to have always the same structure... well... actually that's not a good approach, period.
I have the following JSON and I need to get the plain name value using JSONPath:
{
"single" : {
"id" : 1,
"name" : "Item name"
}
}
Expression that I used is $.single.name but I always get an array:
[ "Item name" ]
instead of a string value ("Item name").
but I always get an array:
That is meant to happen. As you can read in this documentation, under 'Result' (almost at the bottom):
Please note, that the return value of jsonPath is an array, which is
also a valid JSON structure. So you might want to apply jsonPath to
the resulting structure again or use one of your favorite array
methods as sort with it.
So basically it will always return an array. If you need the data as an other type, e.g. a String in this case, you will have to do the conversion yourself I'm afraid.
I was using the Java implementation of JSONPath and got to the very same issue. What worked for me was to add '[0]' to the json path string. So in your case:
$.single.name[0]
I think, it depends of language implementation.
For example in nodejs there is a npm module : https://www.npmjs.com/package/jsonpath
Which have a method called value, which does exactly what we need
jp.value(obj, pathExpression[, newValue])
Returns the value of the first element matching pathExpression. If newValue is provided, sets the value of the first matching element and returns the new value.
I tried it and worked!
In case you got this error
"Unable to get a scalar value for expression $.yourfield"
You have just to configure the EvaluateJsonPath processor by changing the return type property value to 'json' instead of 'auto-detect'
I am trying to retrieve a permissive dictionary using Newtonsoft.Json. Basically, what I the tool is doing is when it has an input like
string jsonText =
#"{
"key1" : "val1",
"key2" : "val2",
"inconsistency",
"key3" : "val3"
}";
However, the
JObject.Parse(jsonText);
command will throw me an JsonReaderException because my Json is incorrect.
I would like to have some kind of method
public JObject PermissiveParse(jsonText)
which will return a JObject with the 3 valid JTokens. (skip the invalid one)
I'm afraid that's a classic "garbage in, garbage out" situation. The
JSON is invalid, and so you can't parse it properly. You can only
guess at what it's meant to be. Now, we humans can guess pretty well
at what was intended (obviously), but that's much more difficult at a
parser level.
If you know that consistently you're getting this invalid jsonText,
you could pre-process the string before deserializing it, but the real
fix has to be at the source of the invalid data.
https://stackoverflow.com/a/5142168/393487
How could I extract values from the following response:
{"photosets":{"cancreate":1, "page":1, "pages":1, "perpage":1, "total":1, "photoset":
[{"id":"72157629171255321", "primary":"6817523143", "secret":"250f42ed7c", "server":"7166",
"farm":8, "photos":"1", "videos":0, "title":{"_content":"Set1"}, "description":{"_content":""},
"needs_interstitial":0, "visibility_can_see_set":1, "count_views":"0", "count_comments":"0",
"can_comment":1, "date_create":"1328373127", "date_update":"1328373159"}]}, "stat":"ok"}
I need some values not All of them for example I need "id" field How can I get it?
Thanks,
There is a very performant wrapper (because Jackson is...) for scala.
Jerkson will help you in doing this check this out.
val ids: Seq[JString] = parse[JValue]("""
{"photosets":{"cancreate":1, "page":1, "pages":1, "perpage":1, "total":1, "photoset":
[{"id":"72157629171255321", "primary":"6817523143", "secret":"250f42ed7c", "server":"7166", "farm":8, "photos":"1", "videos":0, "title":{"_content":"Set1"}, "description": {"_content":""}, "needs_interstitial":0, "visibility_can_see_set":1, "count_views":"0", "count_comments":"0", "can_comment":1, "date_create":"1328373127", "date_update":"1328373159"}]}, "stat":"ok"}
""") \\ "id"
Here is a HowTo, note the use of parse that will return a JValue, on which you can call \ or \\ that find on the first level or any the property given rigth after.
Using reflection, Jerkson is also available to parse directly in case class, check the documentation... it's very powerful
There isn't anything inherently Scala about this, just use a JSON parser like Jackson and extract the information that you need.