I need to parse a JSON with nested array elements and extract the values.
I am not sure how to use the nested array to set the value of an attribute in output JSON.
This is the input:
[{
"name": "book1",
"id": 18789,
"locations": [{
"state": "mystate",
"phone": 8877887700
}, {
"state": "mystate1",
"phone": 8877887701
}]
},
{
"name": "book2",
"id": 18781,
"locations": [{
"state": "mystate3",
"phone": 8877887711
}, {
"state": "mystate4",
"phone": 8877887702
}]
}]
And this is the expected output:
{
"name": ["book1", "book2"],
"id": ["18789", "18781"],
"states": [
["mystate", "mystate"],
["mystate3", "mystate4"]
]
}
I am trying to use the following JSLT expression:
{
"name" : [for (.)
let s = string(.name)
$s],
"id": [for (.)
let s = string(.id)
$s],
"states": [for (.)
let s = string(.locations)
$s]
}
But I am not sure how to set the states
in this case so that I have the value of state in the output.
A solution using JQ or JSONPath may also help.
question from:
https://stackoverflow.com/questions/66047747/how-to-read-nested-array-elements-from-json 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…