how to i select multiple keys using JQ
How can jq extract both the index key and doctype key based on the dynamic attribute?:
"index1":
"mappings":
"doctype1":
"dynamic": "true"
,
"doctype2":
"dynamic": "static"
,
"index2":
"mappings":
"doctype3":
"dynamic": "static"
,
"doctype4":
"dynamic": "true"
to give:
{
"index1":
"doctype2": "dynamic": "static"
,
"index2":
"doctype3": "dynamic": "static"
or
"index1": "['doctype2']",
"index2": "['doctype3']"
I have tried:
jq '. | keys as $i | ..mappings | keys as $d | $i $d'
but it's not even close
json select key jq
add a comment |
How can jq extract both the index key and doctype key based on the dynamic attribute?:
"index1":
"mappings":
"doctype1":
"dynamic": "true"
,
"doctype2":
"dynamic": "static"
,
"index2":
"mappings":
"doctype3":
"dynamic": "static"
,
"doctype4":
"dynamic": "true"
to give:
{
"index1":
"doctype2": "dynamic": "static"
,
"index2":
"doctype3": "dynamic": "static"
or
"index1": "['doctype2']",
"index2": "['doctype3']"
I have tried:
jq '. | keys as $i | ..mappings | keys as $d | $i $d'
but it's not even close
json select key jq
1
Which one is your expected output? Explain that clearly
– Inian
Nov 13 '18 at 12:22
Can you confirm the 2nd output is a string (instead of a JSON array)?
– oliv
Nov 13 '18 at 13:05
@oliv: Accidentally removed part of the edit
– Inian
Nov 13 '18 at 13:21
I species two outputs as I wasn't particular on the exact format/schema, just that extracting two keys was problematic. The conditional was a bonus.
– James Liu
Nov 13 '18 at 14:29
add a comment |
How can jq extract both the index key and doctype key based on the dynamic attribute?:
"index1":
"mappings":
"doctype1":
"dynamic": "true"
,
"doctype2":
"dynamic": "static"
,
"index2":
"mappings":
"doctype3":
"dynamic": "static"
,
"doctype4":
"dynamic": "true"
to give:
{
"index1":
"doctype2": "dynamic": "static"
,
"index2":
"doctype3": "dynamic": "static"
or
"index1": "['doctype2']",
"index2": "['doctype3']"
I have tried:
jq '. | keys as $i | ..mappings | keys as $d | $i $d'
but it's not even close
json select key jq
How can jq extract both the index key and doctype key based on the dynamic attribute?:
"index1":
"mappings":
"doctype1":
"dynamic": "true"
,
"doctype2":
"dynamic": "static"
,
"index2":
"mappings":
"doctype3":
"dynamic": "static"
,
"doctype4":
"dynamic": "true"
to give:
{
"index1":
"doctype2": "dynamic": "static"
,
"index2":
"doctype3": "dynamic": "static"
or
"index1": "['doctype2']",
"index2": "['doctype3']"
I have tried:
jq '. | keys as $i | ..mappings | keys as $d | $i $d'
but it's not even close
json select key jq
json select key jq
edited Nov 13 '18 at 17:52
peak
31.2k83957
31.2k83957
asked Nov 13 '18 at 11:38
James LiuJames Liu
295
295
1
Which one is your expected output? Explain that clearly
– Inian
Nov 13 '18 at 12:22
Can you confirm the 2nd output is a string (instead of a JSON array)?
– oliv
Nov 13 '18 at 13:05
@oliv: Accidentally removed part of the edit
– Inian
Nov 13 '18 at 13:21
I species two outputs as I wasn't particular on the exact format/schema, just that extracting two keys was problematic. The conditional was a bonus.
– James Liu
Nov 13 '18 at 14:29
add a comment |
1
Which one is your expected output? Explain that clearly
– Inian
Nov 13 '18 at 12:22
Can you confirm the 2nd output is a string (instead of a JSON array)?
– oliv
Nov 13 '18 at 13:05
@oliv: Accidentally removed part of the edit
– Inian
Nov 13 '18 at 13:21
I species two outputs as I wasn't particular on the exact format/schema, just that extracting two keys was problematic. The conditional was a bonus.
– James Liu
Nov 13 '18 at 14:29
1
1
Which one is your expected output? Explain that clearly
– Inian
Nov 13 '18 at 12:22
Which one is your expected output? Explain that clearly
– Inian
Nov 13 '18 at 12:22
Can you confirm the 2nd output is a string (instead of a JSON array)?
– oliv
Nov 13 '18 at 13:05
Can you confirm the 2nd output is a string (instead of a JSON array)?
– oliv
Nov 13 '18 at 13:05
@oliv: Accidentally removed part of the edit
– Inian
Nov 13 '18 at 13:21
@oliv: Accidentally removed part of the edit
– Inian
Nov 13 '18 at 13:21
I species two outputs as I wasn't particular on the exact format/schema, just that extracting two keys was problematic. The conditional was a bonus.
– James Liu
Nov 13 '18 at 14:29
I species two outputs as I wasn't particular on the exact format/schema, just that extracting two keys was problematic. The conditional was a bonus.
– James Liu
Nov 13 '18 at 14:29
add a comment |
2 Answers
2
active
oldest
votes
You can try these filters:
For the first output:
jq '[
tostream|
select(.[1]=="static" and (.[0]|.[-1] == "dynamic"))|
(.[0]
]|add' file
And the 2nd one:
jq '[
tostream|
select(.[1]=="static" and (.[0]|.[-1] == "dynamic"))|
.[0]):[(.[0]
]|add' file
Both filter relies on tostream function (requires at least jq version 1.5) which give the content of the input into array in a form [path,value].
The select function get all paths ending with dynamic and static.
The 3rd part of the filter forge the new object according to the expected output by looking up the array path.
add a comment |
To obtain the output in the first form:
map_values( .mappings
| with_entries( select( .value.dynamic == "static" )) )
We use map_values as the input is a JSON object, and we want to retain its keys; we use with_entries as that makes it easy to select the keys of interest.
The above filter could easily be tweaked to yield the output in the second form, but that form is a bit weird and perhaps not-to-be-encouraged, so I won't.
map_valuesadds an empty object in case there is another object e.g.index3withmappingsanddoctypebut without"dynamic": "static"
– oliv
Nov 13 '18 at 15:18
@oliv - Sure. The requirements are not so clear on this and other points, but if such keys are to be omitted, that can easily be done, e.g. by post-processing.
– peak
Nov 13 '18 at 15:33
Not sure if this is possible, but I actually thought theselectcould better placed. Nice answer anyway +1.
– oliv
Nov 13 '18 at 15:41
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%2f53280221%2fhow-to-i-select-multiple-keys-using-jq%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try these filters:
For the first output:
jq '[
tostream|
select(.[1]=="static" and (.[0]|.[-1] == "dynamic"))|
(.[0]
]|add' file
And the 2nd one:
jq '[
tostream|
select(.[1]=="static" and (.[0]|.[-1] == "dynamic"))|
.[0]):[(.[0]
]|add' file
Both filter relies on tostream function (requires at least jq version 1.5) which give the content of the input into array in a form [path,value].
The select function get all paths ending with dynamic and static.
The 3rd part of the filter forge the new object according to the expected output by looking up the array path.
add a comment |
You can try these filters:
For the first output:
jq '[
tostream|
select(.[1]=="static" and (.[0]|.[-1] == "dynamic"))|
(.[0]
]|add' file
And the 2nd one:
jq '[
tostream|
select(.[1]=="static" and (.[0]|.[-1] == "dynamic"))|
.[0]):[(.[0]
]|add' file
Both filter relies on tostream function (requires at least jq version 1.5) which give the content of the input into array in a form [path,value].
The select function get all paths ending with dynamic and static.
The 3rd part of the filter forge the new object according to the expected output by looking up the array path.
add a comment |
You can try these filters:
For the first output:
jq '[
tostream|
select(.[1]=="static" and (.[0]|.[-1] == "dynamic"))|
(.[0]
]|add' file
And the 2nd one:
jq '[
tostream|
select(.[1]=="static" and (.[0]|.[-1] == "dynamic"))|
.[0]):[(.[0]
]|add' file
Both filter relies on tostream function (requires at least jq version 1.5) which give the content of the input into array in a form [path,value].
The select function get all paths ending with dynamic and static.
The 3rd part of the filter forge the new object according to the expected output by looking up the array path.
You can try these filters:
For the first output:
jq '[
tostream|
select(.[1]=="static" and (.[0]|.[-1] == "dynamic"))|
(.[0]
]|add' file
And the 2nd one:
jq '[
tostream|
select(.[1]=="static" and (.[0]|.[-1] == "dynamic"))|
.[0]):[(.[0]
]|add' file
Both filter relies on tostream function (requires at least jq version 1.5) which give the content of the input into array in a form [path,value].
The select function get all paths ending with dynamic and static.
The 3rd part of the filter forge the new object according to the expected output by looking up the array path.
answered Nov 13 '18 at 13:14
olivoliv
8,3111130
8,3111130
add a comment |
add a comment |
To obtain the output in the first form:
map_values( .mappings
| with_entries( select( .value.dynamic == "static" )) )
We use map_values as the input is a JSON object, and we want to retain its keys; we use with_entries as that makes it easy to select the keys of interest.
The above filter could easily be tweaked to yield the output in the second form, but that form is a bit weird and perhaps not-to-be-encouraged, so I won't.
map_valuesadds an empty object in case there is another object e.g.index3withmappingsanddoctypebut without"dynamic": "static"
– oliv
Nov 13 '18 at 15:18
@oliv - Sure. The requirements are not so clear on this and other points, but if such keys are to be omitted, that can easily be done, e.g. by post-processing.
– peak
Nov 13 '18 at 15:33
Not sure if this is possible, but I actually thought theselectcould better placed. Nice answer anyway +1.
– oliv
Nov 13 '18 at 15:41
add a comment |
To obtain the output in the first form:
map_values( .mappings
| with_entries( select( .value.dynamic == "static" )) )
We use map_values as the input is a JSON object, and we want to retain its keys; we use with_entries as that makes it easy to select the keys of interest.
The above filter could easily be tweaked to yield the output in the second form, but that form is a bit weird and perhaps not-to-be-encouraged, so I won't.
map_valuesadds an empty object in case there is another object e.g.index3withmappingsanddoctypebut without"dynamic": "static"
– oliv
Nov 13 '18 at 15:18
@oliv - Sure. The requirements are not so clear on this and other points, but if such keys are to be omitted, that can easily be done, e.g. by post-processing.
– peak
Nov 13 '18 at 15:33
Not sure if this is possible, but I actually thought theselectcould better placed. Nice answer anyway +1.
– oliv
Nov 13 '18 at 15:41
add a comment |
To obtain the output in the first form:
map_values( .mappings
| with_entries( select( .value.dynamic == "static" )) )
We use map_values as the input is a JSON object, and we want to retain its keys; we use with_entries as that makes it easy to select the keys of interest.
The above filter could easily be tweaked to yield the output in the second form, but that form is a bit weird and perhaps not-to-be-encouraged, so I won't.
To obtain the output in the first form:
map_values( .mappings
| with_entries( select( .value.dynamic == "static" )) )
We use map_values as the input is a JSON object, and we want to retain its keys; we use with_entries as that makes it easy to select the keys of interest.
The above filter could easily be tweaked to yield the output in the second form, but that form is a bit weird and perhaps not-to-be-encouraged, so I won't.
answered Nov 13 '18 at 14:49
peakpeak
31.2k83957
31.2k83957
map_valuesadds an empty object in case there is another object e.g.index3withmappingsanddoctypebut without"dynamic": "static"
– oliv
Nov 13 '18 at 15:18
@oliv - Sure. The requirements are not so clear on this and other points, but if such keys are to be omitted, that can easily be done, e.g. by post-processing.
– peak
Nov 13 '18 at 15:33
Not sure if this is possible, but I actually thought theselectcould better placed. Nice answer anyway +1.
– oliv
Nov 13 '18 at 15:41
add a comment |
map_valuesadds an empty object in case there is another object e.g.index3withmappingsanddoctypebut without"dynamic": "static"
– oliv
Nov 13 '18 at 15:18
@oliv - Sure. The requirements are not so clear on this and other points, but if such keys are to be omitted, that can easily be done, e.g. by post-processing.
– peak
Nov 13 '18 at 15:33
Not sure if this is possible, but I actually thought theselectcould better placed. Nice answer anyway +1.
– oliv
Nov 13 '18 at 15:41
map_values adds an empty object in case there is another object e.g. index3 with mappings and doctype but without "dynamic": "static"– oliv
Nov 13 '18 at 15:18
map_values adds an empty object in case there is another object e.g. index3 with mappings and doctype but without "dynamic": "static"– oliv
Nov 13 '18 at 15:18
@oliv - Sure. The requirements are not so clear on this and other points, but if such keys are to be omitted, that can easily be done, e.g. by post-processing.
– peak
Nov 13 '18 at 15:33
@oliv - Sure. The requirements are not so clear on this and other points, but if such keys are to be omitted, that can easily be done, e.g. by post-processing.
– peak
Nov 13 '18 at 15:33
Not sure if this is possible, but I actually thought the
select could better placed. Nice answer anyway +1.– oliv
Nov 13 '18 at 15:41
Not sure if this is possible, but I actually thought the
select could better placed. Nice answer anyway +1.– oliv
Nov 13 '18 at 15:41
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%2f53280221%2fhow-to-i-select-multiple-keys-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
1
Which one is your expected output? Explain that clearly
– Inian
Nov 13 '18 at 12:22
Can you confirm the 2nd output is a string (instead of a JSON array)?
– oliv
Nov 13 '18 at 13:05
@oliv: Accidentally removed part of the edit
– Inian
Nov 13 '18 at 13:21
I species two outputs as I wasn't particular on the exact format/schema, just that extracting two keys was problematic. The conditional was a bonus.
– James Liu
Nov 13 '18 at 14:29