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;








1















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"










share|improve this question



















  • 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

















1















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"










share|improve this question



















  • 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













1












1








1








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"










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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, 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












  • 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







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










1 Answer
1






active

oldest

votes


















2














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"






share|improve this answer

























  • 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












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
);



);













draft saved

draft discarded


















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









2














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"






share|improve this answer

























  • 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
















2














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"






share|improve this answer

























  • 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














2












2








2







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"






share|improve this answer















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"







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









KusalanandaKusalananda

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 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


















  • 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

















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


















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

NetworkManager fails with “Could not find source connection”Trouble connecting to VPN using network-manager, while command line worksHow can I be notified about state changes to a VPN adapterBacktrack 5 R3 - Refuses to connect to VPNFeed all traffic through OpenVPN for a specific network namespace onlyRun daemon on startup in Debian once openvpn connection establishedpfsense tcp connection between openvpn and lan is brokenInternet connection problem with web browsers onlyWhy does NetworkManager explicitly support tun/tap devices?Browser issues with VPNTwo IP addresses assigned to the same network card - OpenVPN issues?Cannot connect to WiFi with nmcli, although secrets are provided