jq get attribute of nested object
I have the following structure (full file example here):
{
{
"weather": [
{
"advertiser": "Worldwide Forecasts",
"notificationText": "Weather - Check the Weather Now - Weather",
"notificationURL": "http://www.worldwideforecasts.com/b/index.html?aff_sub=NL01R15178755321634q5X2zHqXo1976US&utm_term=weather&utm_campaign=bing&utm_content=new-weather%20-%20Weather%20-%202&utm_medium=Search_SEM&utm_source=7&nlhash=BR6et8ISl5dCdAHF&requestId=NL01R15178755321634q5X2zHqXo1976US",
"uuid": "9f27d258-f9d5-4b19-9f70-291eb9cbab51",
"creativeSetId": "2799cbab-1fa1-4f15-bbc9-c92381dbfc57"
},
{
"advertiser": "Worldwide Forecasts",
"notificationText": "Weather - Check the Weather Now - Weather",
"notificationURL": "http://www.worldwideforecasts.com/b/index.html?aff_sub=NL01R15178755321634q5X2zHqXo1976US&utm_term=weather&utm_campaign=bing&utm_content=new-weather%20-%20Weather%20-%202&utm_medium=Search_SEM&utm_source=7&nlhash=BR6et8ISl5dCdAHF&requestId=NL01R15178755321634q5X2zHqXo1976US",
"uuid": "9f27d258-f9d5-4b19-9f70-291eb9cbab51",
"creativeSetId": "2799cbab-1fa1-4f15-bbc9-c92381dbfc57"
}
]
}
}
With several categories like the weather
example.
I want to use jq
to get a list of all values for notificationURL
so that I'm left with a list of the URLs for all advertisers.
I was initially thinking I could do something like:
cat file.json | jq ...notificationURL
But this doesn't work.
Can anyone assist with the correct way to do this?
jq
add a comment |
I have the following structure (full file example here):
{
{
"weather": [
{
"advertiser": "Worldwide Forecasts",
"notificationText": "Weather - Check the Weather Now - Weather",
"notificationURL": "http://www.worldwideforecasts.com/b/index.html?aff_sub=NL01R15178755321634q5X2zHqXo1976US&utm_term=weather&utm_campaign=bing&utm_content=new-weather%20-%20Weather%20-%202&utm_medium=Search_SEM&utm_source=7&nlhash=BR6et8ISl5dCdAHF&requestId=NL01R15178755321634q5X2zHqXo1976US",
"uuid": "9f27d258-f9d5-4b19-9f70-291eb9cbab51",
"creativeSetId": "2799cbab-1fa1-4f15-bbc9-c92381dbfc57"
},
{
"advertiser": "Worldwide Forecasts",
"notificationText": "Weather - Check the Weather Now - Weather",
"notificationURL": "http://www.worldwideforecasts.com/b/index.html?aff_sub=NL01R15178755321634q5X2zHqXo1976US&utm_term=weather&utm_campaign=bing&utm_content=new-weather%20-%20Weather%20-%202&utm_medium=Search_SEM&utm_source=7&nlhash=BR6et8ISl5dCdAHF&requestId=NL01R15178755321634q5X2zHqXo1976US",
"uuid": "9f27d258-f9d5-4b19-9f70-291eb9cbab51",
"creativeSetId": "2799cbab-1fa1-4f15-bbc9-c92381dbfc57"
}
]
}
}
With several categories like the weather
example.
I want to use jq
to get a list of all values for notificationURL
so that I'm left with a list of the URLs for all advertisers.
I was initially thinking I could do something like:
cat file.json | jq ...notificationURL
But this doesn't work.
Can anyone assist with the correct way to do this?
jq
add a comment |
I have the following structure (full file example here):
{
{
"weather": [
{
"advertiser": "Worldwide Forecasts",
"notificationText": "Weather - Check the Weather Now - Weather",
"notificationURL": "http://www.worldwideforecasts.com/b/index.html?aff_sub=NL01R15178755321634q5X2zHqXo1976US&utm_term=weather&utm_campaign=bing&utm_content=new-weather%20-%20Weather%20-%202&utm_medium=Search_SEM&utm_source=7&nlhash=BR6et8ISl5dCdAHF&requestId=NL01R15178755321634q5X2zHqXo1976US",
"uuid": "9f27d258-f9d5-4b19-9f70-291eb9cbab51",
"creativeSetId": "2799cbab-1fa1-4f15-bbc9-c92381dbfc57"
},
{
"advertiser": "Worldwide Forecasts",
"notificationText": "Weather - Check the Weather Now - Weather",
"notificationURL": "http://www.worldwideforecasts.com/b/index.html?aff_sub=NL01R15178755321634q5X2zHqXo1976US&utm_term=weather&utm_campaign=bing&utm_content=new-weather%20-%20Weather%20-%202&utm_medium=Search_SEM&utm_source=7&nlhash=BR6et8ISl5dCdAHF&requestId=NL01R15178755321634q5X2zHqXo1976US",
"uuid": "9f27d258-f9d5-4b19-9f70-291eb9cbab51",
"creativeSetId": "2799cbab-1fa1-4f15-bbc9-c92381dbfc57"
}
]
}
}
With several categories like the weather
example.
I want to use jq
to get a list of all values for notificationURL
so that I'm left with a list of the URLs for all advertisers.
I was initially thinking I could do something like:
cat file.json | jq ...notificationURL
But this doesn't work.
Can anyone assist with the correct way to do this?
jq
I have the following structure (full file example here):
{
{
"weather": [
{
"advertiser": "Worldwide Forecasts",
"notificationText": "Weather - Check the Weather Now - Weather",
"notificationURL": "http://www.worldwideforecasts.com/b/index.html?aff_sub=NL01R15178755321634q5X2zHqXo1976US&utm_term=weather&utm_campaign=bing&utm_content=new-weather%20-%20Weather%20-%202&utm_medium=Search_SEM&utm_source=7&nlhash=BR6et8ISl5dCdAHF&requestId=NL01R15178755321634q5X2zHqXo1976US",
"uuid": "9f27d258-f9d5-4b19-9f70-291eb9cbab51",
"creativeSetId": "2799cbab-1fa1-4f15-bbc9-c92381dbfc57"
},
{
"advertiser": "Worldwide Forecasts",
"notificationText": "Weather - Check the Weather Now - Weather",
"notificationURL": "http://www.worldwideforecasts.com/b/index.html?aff_sub=NL01R15178755321634q5X2zHqXo1976US&utm_term=weather&utm_campaign=bing&utm_content=new-weather%20-%20Weather%20-%202&utm_medium=Search_SEM&utm_source=7&nlhash=BR6et8ISl5dCdAHF&requestId=NL01R15178755321634q5X2zHqXo1976US",
"uuid": "9f27d258-f9d5-4b19-9f70-291eb9cbab51",
"creativeSetId": "2799cbab-1fa1-4f15-bbc9-c92381dbfc57"
}
]
}
}
With several categories like the weather
example.
I want to use jq
to get a list of all values for notificationURL
so that I'm left with a list of the URLs for all advertisers.
I was initially thinking I could do something like:
cat file.json | jq ...notificationURL
But this doesn't work.
Can anyone assist with the correct way to do this?
jq
jq
asked 3 hours ago
Philip Kirkbride
2,3932782
2,3932782
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You have two basic options here (with your full file):
jq '.categories.notificationURL' bundle.json
jq '.categories | ..notificationURL' bundle.json
In the first, you apply the operation twice in succession, and in the second you compose two separate filters together with the pipe operator. I find using extra pipes a lot easier to work with and debug, but these are both identical for your purpose.
.
generic iteration always applies to the single input value of this iteration, so it has to be the first element in an expression and is a syntax error anywhere else, but applied to a specific value to its left-hand side is fine anywhere.
The very fact that you can write abc
at all, rather than only .
, is not well-described in the jq documentation, but you can. You can think of .
as applying to the identity filter
.
, although I think it is actually a special case.
In this exact case you can shrink it down to:
jq '..notificationURL' bundle.json
which applies the iterator operation thrice in a row, since there's only the one key "categories" in the top-level object.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f491669%2fjq-get-attribute-of-nested-object%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
You have two basic options here (with your full file):
jq '.categories.notificationURL' bundle.json
jq '.categories | ..notificationURL' bundle.json
In the first, you apply the operation twice in succession, and in the second you compose two separate filters together with the pipe operator. I find using extra pipes a lot easier to work with and debug, but these are both identical for your purpose.
.
generic iteration always applies to the single input value of this iteration, so it has to be the first element in an expression and is a syntax error anywhere else, but applied to a specific value to its left-hand side is fine anywhere.
The very fact that you can write abc
at all, rather than only .
, is not well-described in the jq documentation, but you can. You can think of .
as applying to the identity filter
.
, although I think it is actually a special case.
In this exact case you can shrink it down to:
jq '..notificationURL' bundle.json
which applies the iterator operation thrice in a row, since there's only the one key "categories" in the top-level object.
add a comment |
You have two basic options here (with your full file):
jq '.categories.notificationURL' bundle.json
jq '.categories | ..notificationURL' bundle.json
In the first, you apply the operation twice in succession, and in the second you compose two separate filters together with the pipe operator. I find using extra pipes a lot easier to work with and debug, but these are both identical for your purpose.
.
generic iteration always applies to the single input value of this iteration, so it has to be the first element in an expression and is a syntax error anywhere else, but applied to a specific value to its left-hand side is fine anywhere.
The very fact that you can write abc
at all, rather than only .
, is not well-described in the jq documentation, but you can. You can think of .
as applying to the identity filter
.
, although I think it is actually a special case.
In this exact case you can shrink it down to:
jq '..notificationURL' bundle.json
which applies the iterator operation thrice in a row, since there's only the one key "categories" in the top-level object.
add a comment |
You have two basic options here (with your full file):
jq '.categories.notificationURL' bundle.json
jq '.categories | ..notificationURL' bundle.json
In the first, you apply the operation twice in succession, and in the second you compose two separate filters together with the pipe operator. I find using extra pipes a lot easier to work with and debug, but these are both identical for your purpose.
.
generic iteration always applies to the single input value of this iteration, so it has to be the first element in an expression and is a syntax error anywhere else, but applied to a specific value to its left-hand side is fine anywhere.
The very fact that you can write abc
at all, rather than only .
, is not well-described in the jq documentation, but you can. You can think of .
as applying to the identity filter
.
, although I think it is actually a special case.
In this exact case you can shrink it down to:
jq '..notificationURL' bundle.json
which applies the iterator operation thrice in a row, since there's only the one key "categories" in the top-level object.
You have two basic options here (with your full file):
jq '.categories.notificationURL' bundle.json
jq '.categories | ..notificationURL' bundle.json
In the first, you apply the operation twice in succession, and in the second you compose two separate filters together with the pipe operator. I find using extra pipes a lot easier to work with and debug, but these are both identical for your purpose.
.
generic iteration always applies to the single input value of this iteration, so it has to be the first element in an expression and is a syntax error anywhere else, but applied to a specific value to its left-hand side is fine anywhere.
The very fact that you can write abc
at all, rather than only .
, is not well-described in the jq documentation, but you can. You can think of .
as applying to the identity filter
.
, although I think it is actually a special case.
In this exact case you can shrink it down to:
jq '..notificationURL' bundle.json
which applies the iterator operation thrice in a row, since there's only the one key "categories" in the top-level object.
answered 2 hours ago
Michael Homer
45.6k8121160
45.6k8121160
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2funix.stackexchange.com%2fquestions%2f491669%2fjq-get-attribute-of-nested-object%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