jq (command-line JSON processor) merge multiple JSON strings from STDINhow to capture the properties tag from json fileHow to replace multiple lines with “sed” and convert to JSON?Using multiple wildcards in jq to select objects in a JSON fileMerging four new line delimited json files using jqSplit a json array to multiple filesExtract .json from a text file with arbitrary textjq - add objects from file into json arrayHow to merge json files using jq or any tool?jq: parse json file with constraint from other fieldhow to parse specific values from multiple JSON objects into csv using jq
Can a virus destroy the BIOS of a modern computer?
What does it mean to describe someone as a butt steak?
What exploit are these user agents trying to use?
How to take photos in burst mode, without vibration?
Has there ever been an airliner design involving reducing generator load by installing solar panels?
Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?
Why is it a bad idea to hire a hitman to eliminate most corrupt politicians?
What reasons are there for a Capitalist to oppose a 100% inheritance tax?
Will google still index a page if I use a $_SESSION variable?
Western buddy movie with a supernatural twist where a woman turns into an eagle at the end
Can I use a neutral wire from another outlet to repair a broken neutral?
Fully-Firstable Anagram Sets
Were any external disk drives stacked vertically?
Stopping power of mountain vs road bike
How could indestructible materials be used in power generation?
If human space travel is limited by the G force vulnerability, is there a way to counter G forces?
Can I ask the recruiters in my resume to put the reason why I am rejected?
RG-213 Cable with electric strained wire as metallic shield of Coaxial cable
How can I make my BBEG immortal short of making them a Lich or Vampire?
What mechanic is there to disable a threat instead of killing it?
Emailing HOD to enhance faculty application
What to put in ESTA if staying in US for a few days before going on to Canada
Blender 2.8 I can't see vertices, edges or faces in edit mode
90's TV series where a boy goes to another dimension through portal near power lines
jq (command-line JSON processor) merge multiple JSON strings from STDIN
how to capture the properties tag from json fileHow to replace multiple lines with “sed” and convert to JSON?Using multiple wildcards in jq to select objects in a JSON fileMerging four new line delimited json files using jqSplit a json array to multiple filesExtract .json from a text file with arbitrary textjq - add objects from file into json arrayHow to merge json files using jq or any tool?jq: parse json file with constraint from other fieldhow to parse specific values from multiple JSON objects into csv using jq
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to merge two different JSON strings from STDIN (using pipe, not files) using jq command. Here's the command trying:
curl ipinfo.io api.ipify.org/?format=json 2>/dev/null | jq -s
Output:
[
"ip": "139.162.244.103",
"hostname": "businessproservices.com",
"city": "London",
"region": "England",
"country": "GB",
"loc": "51.5142,-0.0931",
"postal": "EC2V",
"org": "AS63949 Linode, LLC"
]
"ip": "139.162.244.103"
Expected output:
"ip": "139.162.244.103",
"hostname": "businessproservices.com",
"city": "London",
"region": "England",
"country": "GB",
"loc": "51.5142,-0.0931",
"postal": "EC2V",
"org": "AS63949 Linode, LLC"
json jq
|
show 3 more comments
I am trying to merge two different JSON strings from STDIN (using pipe, not files) using jq command. Here's the command trying:
curl ipinfo.io api.ipify.org/?format=json 2>/dev/null | jq -s
Output:
[
"ip": "139.162.244.103",
"hostname": "businessproservices.com",
"city": "London",
"region": "England",
"country": "GB",
"loc": "51.5142,-0.0931",
"postal": "EC2V",
"org": "AS63949 Linode, LLC"
]
"ip": "139.162.244.103"
Expected output:
"ip": "139.162.244.103",
"hostname": "businessproservices.com",
"city": "London",
"region": "England",
"country": "GB",
"loc": "51.5142,-0.0931",
"postal": "EC2V",
"org": "AS63949 Linode, LLC"
json jq
1
You can't have multiple identical keys in a JSON object. Also, fromjq -s
you would get a single array with two entries, not an array and an object.
– Kusalananda♦
2 days ago
@Kusalananda Oh sorry, I have edited the question, thanks
– Pradeep
2 days ago
curl ipinfo.io | jq
will return expected output.
– RoVo
2 days ago
Can you confirm that you don't get a single array as output from yourjq -s
command?jq -s
should return[ ..., ... ]
, not[ ... ] ...
(which is what you are showing).
– Kusalananda♦
2 days ago
1
@Kusalananda it's working perfectly with jq-1.6 :-) Thanks a lot.
– Pradeep
2 days ago
|
show 3 more comments
I am trying to merge two different JSON strings from STDIN (using pipe, not files) using jq command. Here's the command trying:
curl ipinfo.io api.ipify.org/?format=json 2>/dev/null | jq -s
Output:
[
"ip": "139.162.244.103",
"hostname": "businessproservices.com",
"city": "London",
"region": "England",
"country": "GB",
"loc": "51.5142,-0.0931",
"postal": "EC2V",
"org": "AS63949 Linode, LLC"
]
"ip": "139.162.244.103"
Expected output:
"ip": "139.162.244.103",
"hostname": "businessproservices.com",
"city": "London",
"region": "England",
"country": "GB",
"loc": "51.5142,-0.0931",
"postal": "EC2V",
"org": "AS63949 Linode, LLC"
json jq
I am trying to merge two different JSON strings from STDIN (using pipe, not files) using jq command. Here's the command trying:
curl ipinfo.io api.ipify.org/?format=json 2>/dev/null | jq -s
Output:
[
"ip": "139.162.244.103",
"hostname": "businessproservices.com",
"city": "London",
"region": "England",
"country": "GB",
"loc": "51.5142,-0.0931",
"postal": "EC2V",
"org": "AS63949 Linode, LLC"
]
"ip": "139.162.244.103"
Expected output:
"ip": "139.162.244.103",
"hostname": "businessproservices.com",
"city": "London",
"region": "England",
"country": "GB",
"loc": "51.5142,-0.0931",
"postal": "EC2V",
"org": "AS63949 Linode, LLC"
json jq
json jq
edited 2 days ago
Pradeep
asked 2 days ago
PradeepPradeep
1085
1085
1
You can't have multiple identical keys in a JSON object. Also, fromjq -s
you would get a single array with two entries, not an array and an object.
– Kusalananda♦
2 days ago
@Kusalananda Oh sorry, I have edited the question, thanks
– Pradeep
2 days ago
curl ipinfo.io | jq
will return expected output.
– RoVo
2 days ago
Can you confirm that you don't get a single array as output from yourjq -s
command?jq -s
should return[ ..., ... ]
, not[ ... ] ...
(which is what you are showing).
– Kusalananda♦
2 days ago
1
@Kusalananda it's working perfectly with jq-1.6 :-) Thanks a lot.
– Pradeep
2 days ago
|
show 3 more comments
1
You can't have multiple identical keys in a JSON object. Also, fromjq -s
you would get a single array with two entries, not an array and an object.
– Kusalananda♦
2 days ago
@Kusalananda Oh sorry, I have edited the question, thanks
– Pradeep
2 days ago
curl ipinfo.io | jq
will return expected output.
– RoVo
2 days ago
Can you confirm that you don't get a single array as output from yourjq -s
command?jq -s
should return[ ..., ... ]
, not[ ... ] ...
(which is what you are showing).
– Kusalananda♦
2 days ago
1
@Kusalananda it's working perfectly with jq-1.6 :-) Thanks a lot.
– Pradeep
2 days ago
1
1
You can't have multiple identical keys in a JSON object. Also, from
jq -s
you would get a single array with two entries, not an array and an object.– Kusalananda♦
2 days ago
You can't have multiple identical keys in a JSON object. Also, from
jq -s
you would get a single array with two entries, not an array and an object.– Kusalananda♦
2 days ago
@Kusalananda Oh sorry, I have edited the question, thanks
– Pradeep
2 days ago
@Kusalananda Oh sorry, I have edited the question, thanks
– Pradeep
2 days ago
curl ipinfo.io | jq
will return expected output.– RoVo
2 days ago
curl ipinfo.io | jq
will return expected output.– RoVo
2 days ago
Can you confirm that you don't get a single array as output from your
jq -s
command? jq -s
should return [ ..., ... ]
, not [ ... ] ...
(which is what you are showing).– Kusalananda♦
2 days ago
Can you confirm that you don't get a single array as output from your
jq -s
command? jq -s
should return [ ..., ... ]
, not [ ... ] ...
(which is what you are showing).– Kusalananda♦
2 days ago
1
1
@Kusalananda it's working perfectly with jq-1.6 :-) Thanks a lot.
– Pradeep
2 days ago
@Kusalananda it's working perfectly with jq-1.6 :-) Thanks a lot.
– Pradeep
2 days ago
|
show 3 more comments
1 Answer
1
active
oldest
votes
Your curl
command queries two hosts. Each will return a JSON document. jq -s
will add these together as two entries in an array. To get the first entry (which is what you're presenting as the expected output), simply request .[0]
from jq
, as in
curl -s ipinfo.io 'api.ipify.org/?format=json' | jq -s '.[0]'
or just use the first host from the start:
curl ipinfo.io
To actually merge the two document, apply the jq
command add
to the returned list:
curl -s ipinfo.io 'api.ipify.org/?format=json' | jq -s 'add'
Note that since a JSON object can't contain multiple keys with the same name, any later key will replace an equivalent earlier key, so that if your jq -s
document is
[
"ip": "139.162.244.103",
"hostname": "businessproservices.com",
"city": "London",
"region": "England",
"country": "GB",
"loc": "51.5142,-0.0931",
"postal": "EC2V",
"org": "AS63949 Linode, LLC"
,
"ip": "39.62.44.1",
"country": "UK"
]
then this would be merged as
"ip": "39.62.44.1",
"hostname": "businessproservices.com",
"city": "London",
"region": "England",
"country": "UK",
"loc": "51.5142,-0.0931",
"postal": "EC2V",
"org": "AS63949 Linode, LLC"
Is this solution working for you? I had tried this but doesn't work for me, my jq version is jq-1.5-1-a5b5cbe
– Pradeep
2 days ago
@Pradeep Withjq -s 'add'
? Yes, it works for me usingjq
1.6. I can't unfortunately test with an earlier version easily.
– Kusalananda♦
2 days ago
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%2f510065%2fjq-command-line-json-processor-merge-multiple-json-strings-from-stdin%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
Your curl
command queries two hosts. Each will return a JSON document. jq -s
will add these together as two entries in an array. To get the first entry (which is what you're presenting as the expected output), simply request .[0]
from jq
, as in
curl -s ipinfo.io 'api.ipify.org/?format=json' | jq -s '.[0]'
or just use the first host from the start:
curl ipinfo.io
To actually merge the two document, apply the jq
command add
to the returned list:
curl -s ipinfo.io 'api.ipify.org/?format=json' | jq -s 'add'
Note that since a JSON object can't contain multiple keys with the same name, any later key will replace an equivalent earlier key, so that if your jq -s
document is
[
"ip": "139.162.244.103",
"hostname": "businessproservices.com",
"city": "London",
"region": "England",
"country": "GB",
"loc": "51.5142,-0.0931",
"postal": "EC2V",
"org": "AS63949 Linode, LLC"
,
"ip": "39.62.44.1",
"country": "UK"
]
then this would be merged as
"ip": "39.62.44.1",
"hostname": "businessproservices.com",
"city": "London",
"region": "England",
"country": "UK",
"loc": "51.5142,-0.0931",
"postal": "EC2V",
"org": "AS63949 Linode, LLC"
Is this solution working for you? I had tried this but doesn't work for me, my jq version is jq-1.5-1-a5b5cbe
– Pradeep
2 days ago
@Pradeep Withjq -s 'add'
? Yes, it works for me usingjq
1.6. I can't unfortunately test with an earlier version easily.
– Kusalananda♦
2 days ago
add a comment |
Your curl
command queries two hosts. Each will return a JSON document. jq -s
will add these together as two entries in an array. To get the first entry (which is what you're presenting as the expected output), simply request .[0]
from jq
, as in
curl -s ipinfo.io 'api.ipify.org/?format=json' | jq -s '.[0]'
or just use the first host from the start:
curl ipinfo.io
To actually merge the two document, apply the jq
command add
to the returned list:
curl -s ipinfo.io 'api.ipify.org/?format=json' | jq -s 'add'
Note that since a JSON object can't contain multiple keys with the same name, any later key will replace an equivalent earlier key, so that if your jq -s
document is
[
"ip": "139.162.244.103",
"hostname": "businessproservices.com",
"city": "London",
"region": "England",
"country": "GB",
"loc": "51.5142,-0.0931",
"postal": "EC2V",
"org": "AS63949 Linode, LLC"
,
"ip": "39.62.44.1",
"country": "UK"
]
then this would be merged as
"ip": "39.62.44.1",
"hostname": "businessproservices.com",
"city": "London",
"region": "England",
"country": "UK",
"loc": "51.5142,-0.0931",
"postal": "EC2V",
"org": "AS63949 Linode, LLC"
Is this solution working for you? I had tried this but doesn't work for me, my jq version is jq-1.5-1-a5b5cbe
– Pradeep
2 days ago
@Pradeep Withjq -s 'add'
? Yes, it works for me usingjq
1.6. I can't unfortunately test with an earlier version easily.
– Kusalananda♦
2 days ago
add a comment |
Your curl
command queries two hosts. Each will return a JSON document. jq -s
will add these together as two entries in an array. To get the first entry (which is what you're presenting as the expected output), simply request .[0]
from jq
, as in
curl -s ipinfo.io 'api.ipify.org/?format=json' | jq -s '.[0]'
or just use the first host from the start:
curl ipinfo.io
To actually merge the two document, apply the jq
command add
to the returned list:
curl -s ipinfo.io 'api.ipify.org/?format=json' | jq -s 'add'
Note that since a JSON object can't contain multiple keys with the same name, any later key will replace an equivalent earlier key, so that if your jq -s
document is
[
"ip": "139.162.244.103",
"hostname": "businessproservices.com",
"city": "London",
"region": "England",
"country": "GB",
"loc": "51.5142,-0.0931",
"postal": "EC2V",
"org": "AS63949 Linode, LLC"
,
"ip": "39.62.44.1",
"country": "UK"
]
then this would be merged as
"ip": "39.62.44.1",
"hostname": "businessproservices.com",
"city": "London",
"region": "England",
"country": "UK",
"loc": "51.5142,-0.0931",
"postal": "EC2V",
"org": "AS63949 Linode, LLC"
Your curl
command queries two hosts. Each will return a JSON document. jq -s
will add these together as two entries in an array. To get the first entry (which is what you're presenting as the expected output), simply request .[0]
from jq
, as in
curl -s ipinfo.io 'api.ipify.org/?format=json' | jq -s '.[0]'
or just use the first host from the start:
curl ipinfo.io
To actually merge the two document, apply the jq
command add
to the returned list:
curl -s ipinfo.io 'api.ipify.org/?format=json' | jq -s 'add'
Note that since a JSON object can't contain multiple keys with the same name, any later key will replace an equivalent earlier key, so that if your jq -s
document is
[
"ip": "139.162.244.103",
"hostname": "businessproservices.com",
"city": "London",
"region": "England",
"country": "GB",
"loc": "51.5142,-0.0931",
"postal": "EC2V",
"org": "AS63949 Linode, LLC"
,
"ip": "39.62.44.1",
"country": "UK"
]
then this would be merged as
"ip": "39.62.44.1",
"hostname": "businessproservices.com",
"city": "London",
"region": "England",
"country": "UK",
"loc": "51.5142,-0.0931",
"postal": "EC2V",
"org": "AS63949 Linode, LLC"
edited 2 days ago
answered 2 days ago
Kusalananda♦Kusalananda
139k17261433
139k17261433
Is this solution working for you? I had tried this but doesn't work for me, my jq version is jq-1.5-1-a5b5cbe
– Pradeep
2 days ago
@Pradeep Withjq -s 'add'
? Yes, it works for me usingjq
1.6. I can't unfortunately test with an earlier version easily.
– Kusalananda♦
2 days ago
add a comment |
Is this solution working for you? I had tried this but doesn't work for me, my jq version is jq-1.5-1-a5b5cbe
– Pradeep
2 days ago
@Pradeep Withjq -s 'add'
? Yes, it works for me usingjq
1.6. I can't unfortunately test with an earlier version easily.
– Kusalananda♦
2 days ago
Is this solution working for you? I had tried this but doesn't work for me, my jq version is jq-1.5-1-a5b5cbe
– Pradeep
2 days ago
Is this solution working for you? I had tried this but doesn't work for me, my jq version is jq-1.5-1-a5b5cbe
– Pradeep
2 days ago
@Pradeep With
jq -s 'add'
? Yes, it works for me using jq
1.6. I can't unfortunately test with an earlier version easily.– Kusalananda♦
2 days ago
@Pradeep With
jq -s 'add'
? Yes, it works for me using jq
1.6. I can't unfortunately test with an earlier version easily.– Kusalananda♦
2 days ago
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.
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%2f510065%2fjq-command-line-json-processor-merge-multiple-json-strings-from-stdin%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
You can't have multiple identical keys in a JSON object. Also, from
jq -s
you would get a single array with two entries, not an array and an object.– Kusalananda♦
2 days ago
@Kusalananda Oh sorry, I have edited the question, thanks
– Pradeep
2 days ago
curl ipinfo.io | jq
will return expected output.– RoVo
2 days ago
Can you confirm that you don't get a single array as output from your
jq -s
command?jq -s
should return[ ..., ... ]
, not[ ... ] ...
(which is what you are showing).– Kusalananda♦
2 days ago
1
@Kusalananda it's working perfectly with jq-1.6 :-) Thanks a lot.
– Pradeep
2 days ago