Mongodb/Mongo Query: How do I deserialize nested JSON stored in a string field? [duplicate] - json

This question already has answers here:
MongoDB: How to query over a json string?
(1 answer)
Using stored JavaScript functions in the Aggregation pipeline, MapReduce or runCommand
(1 answer)
Closed 3 years ago.
In the context of metabase:
I am applying the following one-liner query to extract a particular field from a collection of mongodb documents:
[{"$project":{"myName":"$field1.field2" }},
{"$match":{"_id":{"$eq":"blah"}}} ]
myName is a string field that is in fact a serialized JSON object.
How can I adapt the pipeline above, perhaps using JSON.parse(), to pull out fields nested further down the JSON hierarchy?
Part II: How would I add a final step to extract some of those deeply nested fields? The situation is further complicated because the document structure is not consistent between documents...
Thanks!

Related

Convert one line json into multiple lines to write to a file (Ruby) [duplicate]

This question already has answers here:
How to "pretty" format JSON output in Ruby on Rails
(20 answers)
Closed 3 years ago.
I've run into a situation where I'm retrieving a json string in ruby, but I'm attempting to write it to a file structured like your usual json file (for example:
{
"top":{
"mid1":"bot1",
"mid2":"bot2"
}
}
However, the way the json string is structured is like so:
{"top":{"mid1":"bot1","mid2":"bot2"}}
I've seen other posts mention doing JSON.parse in order to get what I want, however that just causes it to look like this:
{"top"=>{"mid1"=>"bot1", "mid2"=>"bot2"}}
Is there a way that I can just convert the json string into what it looks like in the first code block above?
The first representation is JSON. The decoded version is Ruby's internal representation. If you want to go back to JSON, JSON.dump(...) will reconvert it.
Keep in mind this is just presentation. The data is the same.
You may be interested in the pp method as that produces nice, structured debugging output.

Pass String with Json data to Map in Golang [duplicate]

This question already has answers here:
Golang parse JSON array into data structure
(3 answers)
Closed 5 years ago.
Currently I have stored in my database json objects as string. I want to pass them to a map to be able to consult any field as:
Mymap["Name"]
Mymap["Age"]
..
Let's say that my string would be something like:
'{"Name":["zero"],"Age":"10"}'
I don't know the structure of the data, so that Json can have many fields as required and also many levels of nestings (but I am worried more about to get at least the first level)
If you're dealing with a json object of arbitrary structure you can use a map of interfaces as the type to unmarshal it into.
map[string]interface{}
The encoding/json package will nicely unmarshal the json object into it, nested or not.
This, while very flexible, has an obvious downside, the types of the map's values are unknown and so to do anything useful with them you'll have to use a type assertion or type switch.
v, ok := m["key"].(Type)
https://play.golang.org/p/wM0gkU1g5G

JMeter JSON Extractor to get values as an array [duplicate]

This question already has an answer here:
JMeter JSON Extractor, extract all values of one key in a string
(1 answer)
Closed 5 years ago.
I would like to extract variable from a JSON output as an array of values. I use JSON extractor on Main samples and sub-samples. I got two variables, when I check it with a Debug sampler: myvariable has a single value and myvariable__matchNr=25. There are 25 occurences of this variable, so I need all the 25 values too.
Option Match No. in JSON extractor has to be -1 to get all the values with variable name: myvariable_X.

Multiple Lines in Json file [duplicate]

This question already has answers here:
pretty-print JSON using JavaScript
(31 answers)
Closed 5 years ago.
I am storing some query in json file. the query is little bit lengthy. I want to store these query in multiple lines to show query clearly. In C# when we have like this query we put in the front of query # .. Is there any special key to put in the front of json string?
below is image what I mean by this.
No, there is no way to do this; newlines are not allowed in JSON strings [1].

How to get string in JSON? [duplicate]

This question already has answers here:
Jmeter extracting fields/parsing JSON response
(6 answers)
Closed 6 years ago.
I have the JSON like this:
{"message":"[{\"file_name\":\"1464312906_84174_upload.jpg\",\"file_url\":\"uploads\\/tmp\\/1464312906_84174_upload.jpg\"}]","code":200}
How to get the string "1464312906_84174_upload.jpg" by JSON Expression?
message appears to be a JSON-encoded array. So parse it and then access the property you want.
JSON.parse(data.message)[0].file_name
Replace data with the variable containing the object you showed in the question.