How to read nested json file with several roots in extjs - json

How to read the following json file and display in extjs grid columns?
{
"users": [
{
"id": 123,
"name": "Ed",
"orders": [
{
"id": 50,
"total": 100,
}
]
}
]
}
Could someone explain how to read the nested data in the json? I have tried a lot of options like renderer functions, using the '.' property, etc.
Edit: I would like to read the data within "orders", which are "id" and "total".

You are trying to read in an array nested in an object nested in an array.
So your root would have to be users[0].orders
Is that really what you are trying to do?
Working code:
http://jsfiddle.net/el_chief/s6Ynp/1/
Why is your code nested like this?

Related

Dart Json nested object parsing failure

I've got json file with some nested objects inside subs in it:
{
"version": 1,
"data": [{
"married": true,
"subs":[
{
"name":{
"sub1":{}
**},**
},
]
},
]}
If I add another 'name' object (with comma as separator), jsonDecode returns nothing.
if there goes single object, without comma - it's ok.
My Json structure is correct, and it's not restricted to use nested objects at all. Please anyone help.
This line has problem **},** and if changed to }, the problem will be solved..
you can check your json by online tools (e.g https://jsonformatter.curiousconcept.com/#)

JSONPath to get multiple values from nested json

I have a nested JSON in a field that contains multiple important keys that I would like to retrieve as an array:
{
"tasks": [
{
"id": "task_1",
"name": "task_1_name",
"assignees": [
{
"id": "assignee_1",
"name": "assignee_1_name"
}
]
},
{
"id": "task_2",
"name": "task_2_name",
"assignees": [
{
"id": "assignee_2",
"name": "assignee_2_name"
},
{
"id": "assignee_3",
"name": "assignee_3_name"
}
]}]}
All the queries that I've tried so far fx ( $.tasks.*.assignees..id) and many others have returned
[
"assignee_1",
"assignee_2",
"assignee_3"
]
But what I need is:
[
["assignee_1"],
["assignee_2", "assignee_3"]
]
Is it possible to do with JSONPath or any script inside of it, without involving 3rd party tools?
The problem you're facing is that tasks and assignees are arrays. You need to use [*] instead of .* to get the items in the array. So your path should look like
$.tasks[*].assignees[*].id
You can try it at https://json-everything.net/json-path.
NOTE The output from my site will give you both the value and its location within the original document.
Edit
(I didn't read the whole thing :) )
You're not going to be able to get
[
["assignee_1"],
["assignee_2", "assignee_3"]
]
because, as #Tomalak mentioned, JSON Path is a query language. It's going to remove all structure and return only values.

Convert JSON array and keys to JSON dict

I've noticed that some APIs use a format of sending a stripped down version of their data via a JSON array like the following:
[
"joe",
[
5,
2,
"yellow"
]
]
And store a set of keys like the following:
[
"name",
["some_data", [
"favorite_number",
"least_favorite_number",
"car_color"
]]
]
To turn the data from a bunch of random values to a readable set of data, like the following:
{
"name": "joe",
"some_data": {
"favorite_number": 5,
"least_favorite_number": 2,
"car_color": "yellow"
}
}
I was wondering how this could be done? I'd prefer it'd be in python, but I'm fine with programming my own libraries.
After grasping at more straws than I could fit in my mouth, I've figured it out. JSON schema is what I'm supposed to be using!

How to take key value pairs and turn them into an array of objects using JMESPath

I'm looking to change the data structure of a 3rd party API using JMESPath and haven't been able to figure out how to take a single object of key-value pairs and restructure this to an array of objects, each containing key-value pairs.
I've gone through all of the JMESPath docs and examples without finding the specific data structure I've been working with. I've tried using the keys(#) and values(#) built-in functions to grab the key and values of these pairs, but haven't been able to join them together into a single array.
Here is my original JSON data
{
"time": 32,
"terms": {
"192.168.10.121": 84,
"154.223.10.121": 12,
"112.149.10.121": 6
}
}
I'm trying to convert the above JSON data to the following strucutre using JMESPath.
{
"data": [
{ "ip_address": "192.168.10.121", "count": 84 },
{ "ip_address": "154.223.10.121", "count": 12 },
{ "ip_address": "112.149.10.121", "count": 6 }
]}
I've been able to create an array of the keys or an array of values, but not able to create the array of objects containing key-value pairs.
terms.{ data: keys(#)}
terms.{ data: values(#)}
Result when using terms.{ data: keys(#)}
{
"data": [
"192.168.10.121",
"154.223.10.121",
"112.149.10.121"
]}
Result when using terms.{ data: values(#)}
{
"data": [
84,
12,
6
]}
Those two functions seem like the only functions I can use to pull the keys and values from an object containing key-value pairs not originally within an array. From what I can tell, I'm not able to combine both of those functions to output a single array like my example above.
I'm not even sure this is possible using JMESPath. Any expert opinion would be greatly appreciated.
Context
jmespath query language
how to re-normalize data from one structure (schema) to another
how to effectively iterate and filter on object properties (name-value pairs)
Pitfalls
Generally speaking, jmespath is highly flexible in iterating sequentially-indexed arrays, but much less flexible in iterating over object properties
Most transformations with jmespath become extremely cumbersome when the goal is to iterate over object properties
Usually you can produce any arbitrary output in jmespath if you are willing to "manually construct" the output by hand ... this is usually the trade-off when dealing with iterating over object properties (aka name-value pairs)
Example
Given the following original dataset ...
{"time": 32,
"terms": {
"192.168.10.121": 84,
"154.223.10.121": 12,
"112.149.10.121": 6
}
}
... the following jmespath query ...
{"data": [
{ "ip_address": #.terms|keys(#)[0], "count": #.terms|values(#)[0] }
,{ "ip_address": #.terms|keys(#)[1], "count": #.terms|values(#)[1] }
,{ "ip_address": #.terms|keys(#)[2], "count": #.terms|values(#)[2] }
]}
... produces the following result
{"data": [
{ "ip_address": "192.168.10.121", "count": 84 },
{ "ip_address": "154.223.10.121", "count": 12 },
{ "ip_address": "112.149.10.121", "count": 6 }
]}

Extracting data from a JSON file

I have a large JSON file that looks similar to the code below. Is there anyway I can iterate through each object, look for the field "element_type" (it is not present in all objects in the file if that matters) and extract or write each object with the same element type to a file? For example each user would end up in a file called user.json and each book in a file called book.json?
I thought about using javascript but to my knowledge js can't write to files, I also tried to do it using linux command line tools by removing all new lines, then inserting a new line after each "}," and then iterating through each line to find the element type and write it to a file. This worked for most of the data; however, where there were objects like the "problem_type" below, it inserted a new line in the middle of the data due to the nested json in the "times" element. I've run out of ideas at this point.
{
"data": [
{
"element_type": "user",
"first": "John",
"last": "Doe"
},
{
"element_type": "user",
"first": "Lucy",
"last": "Ball"
},
{
"element_type": "book",
"name": "someBook",
"barcode": "111111"
},
{
"element_type": "book",
"name": "bookTwo",
"barcode": "111111"
},
{
"element_type": "problem_type",
"name": "problem object",
"times": "[{\"start\": \"1230\", \"end\": \"1345\", \"day\": \"T\"}, {\"start\": \"1230\", \"end\": \"1345\", \"day\": \"R\"}]"
}
]
}
I would recommend Java for this purpose. It sounds like you're running on Linux so it should be a good fit.
You'll have no problems writing to files. And you can use a library like this - http://json-lib.sourceforge.net/ - to gain access to things like JSONArray and JSONObject. Which you can easily use to iterate through the data in your JSON request, and check what's in "element_type" and write to a file accordingly.