I have this input data :
{
"list": {
"1": {
"attributes": {
"content": [],
"pageable": {
"pageNumber": 0,
"pageSize": 10
}
}
},
"2": {
"attributes": {
"content": [
{
"id": "AZER"
}
],
"pageable": {
"pageNumber": 0,
"pageSize": 10
}
}
},
"3": {
"attributes": {
"content": [
{
"id": "WXC"
},
{
"id": "QSD"
}
],
"pageable": {
"pageNumber": 0,
"pageSize": 10
}
}
}
}
}
I need to use JQ to extract the id values into a new array, I try this:
.list[] | .attributes.content[].id
that’s my results:
"AZER"
"WXC"
"QSD"
It’s what i want but in 3 line and i need to have it in array
How can i make this results in an array like this:
["AZER","WXC","QSD"]
4
Answers
just wrap you jq query with syntax
[${request}]
for sample
in your case the request became
You can populate our own array:
Try it online
In general, just wrap your query that produces a stream of the desired values into array brackets, and the output is an array of those values.
Demo
Note that there’s also a shortcut to
[ .[] … ]
calledmap(…)
:Demo
Both output:
Use the
--compact-output
(or-c
) flag, to remove irrelevant whitespace, thus compacting the output into a single line:["AZER","WXC","QSD"]
If you just want to extract all
"id"
values into an array, streaming is also an option.Try it on jqplay.org.