Diplaying JSON multidimensional array - json

I have this JSON array, but I don't know how display it.
{
"grupy": [{
"id_grupa_parametrow": "1",
"id_grupa_nadrzedna": "0",
"nazwa_grupy": "1_1",
"opis_grupy": "hdghgh",
"kolejnosc": "1233"
}]
}
I tried:
result["grupy"].id_grupa_parametrow;
but it doesn't work.

You can use some online json editors to find your data easily. Anyway, you have an array defined for groupy index. So, use numerical indexes first:
result["grupy"][0].id_grupa_parametrow;

What you have to know is that {} means that it's an object and [] is an array. So result in an object which has grupy property - that's why you have to use a dot here - result.grupy.
grupy is an array to you should use [0] index - result.grupy[0].
And so on...
This is the right way in this case:
result.grupy[0].id_grupa_parametrow;

result.grupy[0].id_grupa_parametrow;

Related

Google Sheet Scripts -> ss.getRange() -> JSON.Stringify removing the brackets at the element level for 1 dimension arrays

If you want to stringify column A,B,C for a few rows it makes sense that JSON.stringify returns something like [ ["1a","2a","3a"], ["1b","2b", "3b"] ].
However if you are using just one column i.e. a 1 dimensional array, then what JSON.stringify does is terrible: [ ["1a"], ["1b"] ]
What my API expects is ["1a","1b"]
What I am missing?: How can I tell it to properly format it?
From the question
However if you are using just one column i.e. a 1 dimensional array, then what JSON.stringify does is terrible: [ ["1a"], ["1b"] ]
It looks that you have a misconception, as getValues() returns a bi-dimensional no matter if the range refers to a single row or a single column. Anyway, one way to convert the bi-dimentional array into a one-dimension array is by using Array.prototype.flat().
let column = [[1],[2],[3]]
console.log(column.flat())

Is it possible to get a sorted list of attribute values from a JSON array using JSONPath

Given JSON like:
[
{
"Serial no": 994,
},
{
"Serial no": 456,
}
]
I know this query will give me an array of all Serial no values, in the order they are in the JSON: $..['Serial no']
I'm not sure exactly what sorting capabilities JSONPath has but I think you can use / and \ to sort - but how are they used to modify my query string in this case? I am only interested doing this in pure JSONPath, not JS or post-query sorting - that's easy, I just want to know if I can avoid it.
This is a source I found suggesting sorting is supported but it might be product-specific?
I'm using http://www.jsonquerytool.com/ to test this

Get Json key using JsonPath

I am struggling to write a JsonPath query to extract particular keys from the following sample Json.
{
"initial": "somevalue",
"somekey2": {
"inner1": "innerval1",
"inner2": "innerval2"
}
}
For example:
1) I wish to extract the first key, which in this case is initial. Is this possible using JsonPath?
2) Get an inner key such as inner1. Something similar to $."initial"."somekey2" but returning an array with just the keys (inner1 and inner2).
This SO question covers it.
$.*~ returns
[
"initial",
"somekey2"
]
$.somekey2.*~ returns
[
"inner1",
"inner2"
]
To get all 2nd order children use $.*.*~. Basically for nth order, $.(n times *).*~
not sure about json path, but suppose your object is a
a={
"initial": "somevalue",
"somekey2": {
"inner1": "innerval1",
"inner2": "innerval2"
}
};
the you can get all keys by using Object.keys(a)
that will give array of keys ["initial", "somekey2"]
then you can use that key to revtrive its nested value
a[Object.keys(a)[1]] // returns {inner1: "innerval1", inner2: "innerval2"}
and you can repeat the same for all nested element

How to extract JSON values that does not have attribute names?

{
"A1":{
"name":"Ad hoc",
"projectId":0
},
"X2":{
"name":"BBB",
"projectId":101
},
"AB":{
"name":"CCC",
"projectId":102
},
"recordsCount":3
}
For this JSON, how to extract values? i need output like,
A1, Ad hoc
X2, BBB
AB, CCC
experts inputs appreciated.
Analysis
XPath can't read unnamed attributes. It will always result in an exception. If you want to get the values, you need to use JsonPath.
Solution
Even then, it makes sense to add surrounding brackets, otherwise the first level will be consumed as well:
[
{
"A1":{
"name":"Ad hoc",
"projectId":0
},
"X2":{
"name":"BBB",
"projectId":101
},
"AB":{
"name":"CCC",
"projectId":102
},
"recordsCount":3
}
]
You can try the correct query on jsonpath.com. For me (with the additional brackets) the path $.* worked.
To extract the values, you need to use a tExtractJSONFields component in Talend if using a file or REST request.
A valid JSON query could be easily [0] for the field with alphanumeric identifiers.

D3 get the value in the middle of nested JSON

Totally noob question from me. Different from other JSON nested questions, I want to access the value of the middle level.
Consider that I have a JSON like:
"nodes":[
{"Level1":[
{"Level2A":[
{"Level3A":"Value",
"Level3B":"Value"
},
{"Level3A":"Value",
"Level3B":"Value"
}]
},
{"Level2B":[
{"Level3A":"Value",
"Level3B":"Value"
},
{"Level3A":"Value",
"Level3B":"Value"
}]
}]
}]
I want to get the value of Level2 out(to use as label).
I can get lv3 value by calling for example,:
node.datum().Level1[0].Level2[0].Level3A
but if I tried
nodae.datum().Level1[].Level2
I get an object instead. The ideal output would be the array with [Level2A, Level2B,...]
Are you sure your json is in valid mode or you just post half of it?
That what I did-
{"nodes":[{"Level1":[{"Level2A":[{"Level3A":"Value","Level3B":"Value"},{"Level3A":"Value", "Level3B":"Value"}],"Level2B":[{"Level3A":"Value","Level3B":"Value"}]}]}]}