Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jq to combine multiple JSON files

First off, I am not an expert with JSON files or with JQ. But here's my problem:

I am simply trying to download to card data (for the MtG card game) through an API, so I can use it in my own spreadsheets etc.

The card data from the API comes in pages, since there is so much of it, and I am trying to find a nice command line method in Windows to combine the files into one. That will make it nice and easy for me to use the information as external data in my workbooks.

The data from the API looks like this:

{
  "object": "list",
  "total_cards": 290,
  "has_more": true,
  "next_page": "https://api.scryfall.com/cards/search?format=json&include_extras=false&order=set&page=2&q=e%3Alea&unique=cards",
  "data": [
    {
      "object": "card",
      "id": "d5c83259-9b90-47c2-b48e-c7d78519e792",
      "oracle_id": "c7a6a165-b709-46e0-ae42-6f69a17c0621",
      "multiverse_ids": [
        232
      ],
      "name": "Animate Wall",
      ......
    }
    {
      "object": "card",
      ......
    }
  ]
}

Basically I need to take what's inside the "data" part from each file after the first, and merge it into the first file.

I have tried a few examples I found online using jq, but I can't get it to work. I think it might be because in this case the data is sort of under an extra level, since there is some basic information, then the "data" category is beneath it. I don't know.

Anyway, any help on how to get this going would be appreciated. I don't know much about this, but I can learn quickly so even any pointers would be great.

Thanks!

like image 397
Jaska Avatar asked Oct 09 '18 22:10

Jaska


People also ask

How do I combine multiple JSON objects into one?

We can merge two JSON objects using the putAll() method (inherited from interface java.

How do I add two JSON files?

If you just want to merge all json files sequentially, go to the folder where all json files are, select all and rename the first one as "yourchoice", by doing this all will be in sequential order i.e. yourchoice1,yourchoice2 ... next go to cmd and type : copy *. json "outputfilename".

What is merge JSON?

json-merge is a library allowing you to merge two json files for the JVM written in Kotlin. It currently supports two modes for merging arrays and objects.

Can jq create JSON?

jq is a command-line utility that can slice, filter, and transform the components of a JSON file.


1 Answers

To merge the .data elements of all the responses into the first response, you could run:

jq 'reduce inputs.data as $s (.; .data += $s)' page1.json page2.json ...

Alternatives

You could use the following filter in conjunction with the -n command-line option:

reduce inputs as $s (input; .data += ($s.data))

Or if you simply want an object of the form {"data": [ ... ]} then (again assuming you invoke jq with the -n command-line option) the following jq filter would suffice:

{data: [inputs.data] | add}
like image 125
peak Avatar answered Sep 20 '22 12:09

peak