Flatten array for properties in nested array of JSON objects using jq
Having JSON with (simplified) Jira data like:
{
"issues": [
{
"key": "TEST-A",
"fields": { "issuelinks": }
},
{
"key": "TEST-B",
"fields": {
"issuelinks": [
{ "inwardIssue": { "key": "TEST-1" } },
{ "outwardIssue": { "key": "TEST-2" } },
{ "outwardIssue": { "key": "TEST-3" } }
]
}
}
]
}
Would like to get output like:
[
{ "key": "TEST-A", "inward": null, "outward": null },
{ "key": "TEST-B", "inward": ["TEST-1"], "outward": ["TEST-2", "TEST-3"] }
]
Tried (ignoring the inward links for now):
cat data.json |
jq '.issues | {"key":.key, "outward":.fields.issuelinks.outwardIssue.key }'
But I get:
{ "key": "TEST-B", "outward": "TEST-1" }
{ "key": "TEST-B", "outward": "TEST-2" }
{ "key": "TEST-B", "outward": null }
Note: would expect 1) TEST-A
for the last one, 2) TEST-2
and TEST-3
for the first two and would like to 3) have TEST-2
and TEST-3
combined in an array.
Suggestions?
json jq flatten
add a comment |
Having JSON with (simplified) Jira data like:
{
"issues": [
{
"key": "TEST-A",
"fields": { "issuelinks": }
},
{
"key": "TEST-B",
"fields": {
"issuelinks": [
{ "inwardIssue": { "key": "TEST-1" } },
{ "outwardIssue": { "key": "TEST-2" } },
{ "outwardIssue": { "key": "TEST-3" } }
]
}
}
]
}
Would like to get output like:
[
{ "key": "TEST-A", "inward": null, "outward": null },
{ "key": "TEST-B", "inward": ["TEST-1"], "outward": ["TEST-2", "TEST-3"] }
]
Tried (ignoring the inward links for now):
cat data.json |
jq '.issues | {"key":.key, "outward":.fields.issuelinks.outwardIssue.key }'
But I get:
{ "key": "TEST-B", "outward": "TEST-1" }
{ "key": "TEST-B", "outward": "TEST-2" }
{ "key": "TEST-B", "outward": null }
Note: would expect 1) TEST-A
for the last one, 2) TEST-2
and TEST-3
for the first two and would like to 3) have TEST-2
and TEST-3
combined in an array.
Suggestions?
json jq flatten
add a comment |
Having JSON with (simplified) Jira data like:
{
"issues": [
{
"key": "TEST-A",
"fields": { "issuelinks": }
},
{
"key": "TEST-B",
"fields": {
"issuelinks": [
{ "inwardIssue": { "key": "TEST-1" } },
{ "outwardIssue": { "key": "TEST-2" } },
{ "outwardIssue": { "key": "TEST-3" } }
]
}
}
]
}
Would like to get output like:
[
{ "key": "TEST-A", "inward": null, "outward": null },
{ "key": "TEST-B", "inward": ["TEST-1"], "outward": ["TEST-2", "TEST-3"] }
]
Tried (ignoring the inward links for now):
cat data.json |
jq '.issues | {"key":.key, "outward":.fields.issuelinks.outwardIssue.key }'
But I get:
{ "key": "TEST-B", "outward": "TEST-1" }
{ "key": "TEST-B", "outward": "TEST-2" }
{ "key": "TEST-B", "outward": null }
Note: would expect 1) TEST-A
for the last one, 2) TEST-2
and TEST-3
for the first two and would like to 3) have TEST-2
and TEST-3
combined in an array.
Suggestions?
json jq flatten
Having JSON with (simplified) Jira data like:
{
"issues": [
{
"key": "TEST-A",
"fields": { "issuelinks": }
},
{
"key": "TEST-B",
"fields": {
"issuelinks": [
{ "inwardIssue": { "key": "TEST-1" } },
{ "outwardIssue": { "key": "TEST-2" } },
{ "outwardIssue": { "key": "TEST-3" } }
]
}
}
]
}
Would like to get output like:
[
{ "key": "TEST-A", "inward": null, "outward": null },
{ "key": "TEST-B", "inward": ["TEST-1"], "outward": ["TEST-2", "TEST-3"] }
]
Tried (ignoring the inward links for now):
cat data.json |
jq '.issues | {"key":.key, "outward":.fields.issuelinks.outwardIssue.key }'
But I get:
{ "key": "TEST-B", "outward": "TEST-1" }
{ "key": "TEST-B", "outward": "TEST-2" }
{ "key": "TEST-B", "outward": null }
Note: would expect 1) TEST-A
for the last one, 2) TEST-2
and TEST-3
for the first two and would like to 3) have TEST-2
and TEST-3
combined in an array.
Suggestions?
json jq flatten
json jq flatten
edited Nov 21 '18 at 20:06
peak
34.1k94161
34.1k94161
asked Nov 21 '18 at 16:20
wivkuwivku
788724
788724
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Let's start with a variation of your first attempt:
.issues
| {key,
inward: .fields.issuelinks|map(.inwardIssue.key // empty),
outward: .fields.issuelinks|map(.outwardIssue.key // empty) }
With your example, this produces:
{"key":"TEST-A","inward":,"outward":}
{"key":"TEST-B","inward":["TEST-1"],"outward":["TEST-2","TEST-3"]}
So two repairs are needed to achieve the stated goal:
- Produce an array (e.g., by wrapping the above expression in square brackets)
- Replace the empty arrays by null.
(2) is dubious but easy to accomplish, e.g. using the helper function defined below.
Solution
def extract(f): map(f // empty) | if length ==0 then null else . end;
.issues
| map(
{key,
inward: .fields.issuelinks|extract(.inwardIssue.key),
outward: .fields.issuelinks|extract(.outwardIssue.key)})
Caveat
If extract
should retain null
and false
values, then its def should be modified accordingly.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416372%2fflatten-array-for-properties-in-nested-array-of-json-objects-using-jq%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Let's start with a variation of your first attempt:
.issues
| {key,
inward: .fields.issuelinks|map(.inwardIssue.key // empty),
outward: .fields.issuelinks|map(.outwardIssue.key // empty) }
With your example, this produces:
{"key":"TEST-A","inward":,"outward":}
{"key":"TEST-B","inward":["TEST-1"],"outward":["TEST-2","TEST-3"]}
So two repairs are needed to achieve the stated goal:
- Produce an array (e.g., by wrapping the above expression in square brackets)
- Replace the empty arrays by null.
(2) is dubious but easy to accomplish, e.g. using the helper function defined below.
Solution
def extract(f): map(f // empty) | if length ==0 then null else . end;
.issues
| map(
{key,
inward: .fields.issuelinks|extract(.inwardIssue.key),
outward: .fields.issuelinks|extract(.outwardIssue.key)})
Caveat
If extract
should retain null
and false
values, then its def should be modified accordingly.
add a comment |
Let's start with a variation of your first attempt:
.issues
| {key,
inward: .fields.issuelinks|map(.inwardIssue.key // empty),
outward: .fields.issuelinks|map(.outwardIssue.key // empty) }
With your example, this produces:
{"key":"TEST-A","inward":,"outward":}
{"key":"TEST-B","inward":["TEST-1"],"outward":["TEST-2","TEST-3"]}
So two repairs are needed to achieve the stated goal:
- Produce an array (e.g., by wrapping the above expression in square brackets)
- Replace the empty arrays by null.
(2) is dubious but easy to accomplish, e.g. using the helper function defined below.
Solution
def extract(f): map(f // empty) | if length ==0 then null else . end;
.issues
| map(
{key,
inward: .fields.issuelinks|extract(.inwardIssue.key),
outward: .fields.issuelinks|extract(.outwardIssue.key)})
Caveat
If extract
should retain null
and false
values, then its def should be modified accordingly.
add a comment |
Let's start with a variation of your first attempt:
.issues
| {key,
inward: .fields.issuelinks|map(.inwardIssue.key // empty),
outward: .fields.issuelinks|map(.outwardIssue.key // empty) }
With your example, this produces:
{"key":"TEST-A","inward":,"outward":}
{"key":"TEST-B","inward":["TEST-1"],"outward":["TEST-2","TEST-3"]}
So two repairs are needed to achieve the stated goal:
- Produce an array (e.g., by wrapping the above expression in square brackets)
- Replace the empty arrays by null.
(2) is dubious but easy to accomplish, e.g. using the helper function defined below.
Solution
def extract(f): map(f // empty) | if length ==0 then null else . end;
.issues
| map(
{key,
inward: .fields.issuelinks|extract(.inwardIssue.key),
outward: .fields.issuelinks|extract(.outwardIssue.key)})
Caveat
If extract
should retain null
and false
values, then its def should be modified accordingly.
Let's start with a variation of your first attempt:
.issues
| {key,
inward: .fields.issuelinks|map(.inwardIssue.key // empty),
outward: .fields.issuelinks|map(.outwardIssue.key // empty) }
With your example, this produces:
{"key":"TEST-A","inward":,"outward":}
{"key":"TEST-B","inward":["TEST-1"],"outward":["TEST-2","TEST-3"]}
So two repairs are needed to achieve the stated goal:
- Produce an array (e.g., by wrapping the above expression in square brackets)
- Replace the empty arrays by null.
(2) is dubious but easy to accomplish, e.g. using the helper function defined below.
Solution
def extract(f): map(f // empty) | if length ==0 then null else . end;
.issues
| map(
{key,
inward: .fields.issuelinks|extract(.inwardIssue.key),
outward: .fields.issuelinks|extract(.outwardIssue.key)})
Caveat
If extract
should retain null
and false
values, then its def should be modified accordingly.
edited Nov 21 '18 at 20:03
answered Nov 21 '18 at 19:58
peakpeak
34.1k94161
34.1k94161
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416372%2fflatten-array-for-properties-in-nested-array-of-json-objects-using-jq%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown