Reputation: 951
I am straggling some time with jq to parse json file. I have to grab ID and Name in the same line so I can do some logic after.
{
"elements": [{
"id": "e20a9cd8-8683-4986-b6c0-e5fbf51cbf7f",
"name": "Mike",
"components": [{
"id": "15f959fc-6d2d-451a-a59e-430a05a1852c",
"pid": "ZZZ1"
}],
"tenantIds": null,
"productIds": null
},
{
"id": "d892f2eb-d7f3-49f8-9176-2113351cccf8",
"name": "Steve",
"components": [{
"id": "0c44c917-e0e5-4fa3-b87c-89f9ac0815b4",
"pid": "XXX3"
}],
"tenantIds": null,
"productIds": null
}
]
}
With jq '{elements}[] | .[].id
I am getting ID's but I can't find solution how to add appropriate name beside ID. I tried something like
{elements}[] | .[].id + " " + .[].name
and
{elements}[] | .[].id + .[].name
but it's not what I expected.
I want to get:
15f959fc-6d2d-451a-a59e-430a05a1852c Mike
d892f2eb-d7f3-49f8-9176-2113351cccf8 Steve
Any suggestion?
Thanks!
Upvotes: 1
Views: 83
Reputation: 92884
Apply string concatenation:
jq '.elements[] | .id +" "+ .name' file
The output:
"e20a9cd8-8683-4986-b6c0-e5fbf51cbf7f Mike"
"d892f2eb-d7f3-49f8-9176-2113351cccf8 Steve"
To output without double quotes use -r
(--raw-output
) option:
jq '.elements[] | .id +" "+ .name' -r file
e20a9cd8-8683-4986-b6c0-e5fbf51cbf7f Mike
d892f2eb-d7f3-49f8-9176-2113351cccf8 Steve
Upvotes: 2