Reporting every (optional) subelement
Reporting every (optional) subelement
Want to parse the following:
{
"issues": [
{ "id": "1", "fields": { "links": } },
{ "id": "2", "fields": { "links": [ { "inlink": { "id": "red" } } ] } },
{ "id": "3", "fields": { "links": [ { "outlink": { "id": "yellow" } } ] } },
{ "id": "4", "fields": { "links": [ { "inlink": { "id": "green" } }, { "outlink": { "id": "blue" } } ] } }
]
}
into elements that represent the links
elements, one for one, like this (note that id 1
is missing and id 4
appears twice:
links
id 1
id 4
{ "id": "2", "link": { "linktype": "inlink", "id": "red" } },
{ "id": "3", "link": { "linktype": "outlink", "id": "yellow" } },
{ "id": "4", "link": { "linktype": "inlink", "id": "green" } },
{ "id": "4", "link": { "linktype": "outlink", "id": "blue" } }
The following jq
works for inlink
or outlink
separately
jq
inlink
outlink
.issues | .id as $id | .fields.links | .inlink.id as $lid | {$id,$lid}
giving
{ "id": "2", "lid": "red" }
{ "id": "3", "lid": null }
{ "id": "4", "lid": "green" }
{ "id": "4", "lid": null }
but can't figure out how to track both types simultaneously or introduce the key linktype
. Any ideas?
linktype
1 Answer
1
jq
solution:
jq
jq -c '.issues | .id as $id
| .fields.links | to_entries
| {"id": $id, "link": {"linktype": .key, "id": .value.id}}' file
The output:
{"id":"2","link":{"linktype":"inlink","id":"red"}}
{"id":"3","link":{"linktype":"outlink","id":"yellow"}}
{"id":"4","link":{"linktype":"inlink","id":"green"}}
{"id":"4","link":{"linktype":"outlink","id":"blue"}}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Comments
Post a Comment