Resolved this on github:

.[0] as $keys |
.[1] as $values |
reduce range(0; $keys|length) as $i  ( {}; . + { ($keys[values[$i] })
Answer from Abdullah Jibaly on Stack Overflow
🌐
GitHub
github.com › jqlang › jq › issues › 684
Creating an array from objects? · Issue #684 · jqlang/jq
January 29, 2015 - First of all, kudos on such an excellent library...I've used jq for basic CLI tasks and have only recently delved into its more advanced functions, and am continually amazed at how things just work with few surprises...rare for a CLI tool that has so many features... So I think my question is pretty basic, and I'm missing something very obvious that could be clarified in the docs. Given a series of objects, what do I pipe them through to get them into an array?
Author   dannguyen
Discussions

Create object from array of keys and values
I've been banging my head against the wall for several hours on this and just can't seem to find a way to do this. I have an array of keys and an array of values, how can I generate an obje... More on github.com
🌐 github.com
5
January 23, 2015
json - How to use jq to create an object with an arbitrary key from a sub array? - Stack Overflow
I can also extract the categories with jq ' .things | .[] | .params | .[] | select(.key == "category") | .value' But I have not been able to combine them. ... Sign up to request clarification or add additional context in comments. ... I think you can shorten e.g. things | .[] to things[] 2019-01-21T07:27:01.02Z+00:00 ... Your params almost looks like key/value entries, so you could create an object out of them by passing the array to from... More on stackoverflow.com
🌐 stackoverflow.com
Add new element to existing JSON array with jq - Stack Overflow
(windows JQ user here): I wonder if it is possible to write this new data to the same (input) file, instead of creating a temporary file which needs to be renamed afterwords.. 2017-08-04T01:20:54.697Z+00:00 ... @script'n'code - Using sponge is probably still the best option if you have it or can install it (it's part of moreutils). 2018-01-31T22:09:23.647Z+00:00 ... @peak how can I insert that object to the beginning of the array... More on stackoverflow.com
🌐 stackoverflow.com
arrays - How to combine the sequence of objects in jq into one object? - Stack Overflow
If the former, which is very often ... modified to output an array by surrounding the iterative processing command with [ and ], rather than having to pipe two jq commands together, the second of which would use -s ... If your input is a stream of objects, then unless your jq has ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › jqlang › jq › issues › 675
Create object from array of keys and values · Issue #675 · jqlang/jq
January 23, 2015 - I've been banging my head against the wall for several hours on this and just can't seem to find a way to do this. I have an array of keys and an array of values, how can I generate an object. Input: [["key1", "key2"], ["val1", "val2"]] ...
Author   amjibaly
🌐
Baeldung
baeldung.com › home › files › file editing › how to add objects into json array using jq
How to Add Objects Into JSON Array Using jq | Baeldung on Linux
March 18, 2024 - Therefore, instead of nesting the array from posts.json into the posts array in blog.json, we used the $inputs[] notation. The $inputs[] construct effectively concatenates the contents of inputs as a flat array directly into posts in blog.json. This results in a single array. In this article, we explored the various ways we can add an object to a JSON array using jq.
🌐
Programming Historian
programminghistorian.org › en › lessons › json-and-jq
Reshaping JSON with jq | Programming Historian
May 24, 2016 - By wrapping . operators within either [] or {}, jq can synthesize new JSON arrays and objects. This can be useful if you want to output a new JSON file. As we will see below, this can also be a crucial intermediate step when reshaping complex JSON. Create a new set of JSON objects with the ...
Find elsewhere
Top answer
1 of 4
192

The |= .+ part in the filter adds a new element to the existing array. You can use jq with filter like:

jq '.data.messages[3] |= . + {
      "date": "2010-01-07T19:55:99.999Z", 
      "xml": "xml_samplesheet_2017_01_07_run_09.xml", 
      "status": "OKKK", 
      "message": "metadata loaded into iRODS successfullyyyyy"
}' inputJson

To avoid using the hardcoded length value 3 and dynamically add a new element, use . | length which returns the length, which can be used as the next array index, i.e.,

jq '.data.messages[.data.messages| length] |= . + {
      "date": "2010-01-07T19:55:99.999Z", 
      "xml": "xml_samplesheet_2017_01_07_run_09.xml", 
      "status": "OKKK", 
      "message": "metadata loaded into iRODS successfullyyyyy"
}' inputJson

(or) as per peak's suggestion in the comments, using the += operator alone

jq '.data.messages += [{
     "date": "2010-01-07T19:55:99.999Z",
     "xml": "xml_samplesheet_2017_01_07_run_09.xml", 
     "status": "OKKK", 
     "message": "metadata loaded into iRODS successfullyyyyy"
}]'

which produces the output you need:

{
  "report": "1.0",
  "data": {
    "date": "2010-01-07",
    "messages": [
      {
        "date": "2010-01-07T19:58:42.949Z",
        "xml": "xml_samplesheet_2017_01_07_run_09.xml",
        "status": "OK",
        "message": "metadata loaded into iRODS successfully"
      },
      {
        "date": "2010-01-07T20:22:46.949Z",
        "xml": "xml_samplesheet_2017_01_07_run_09.xml",
        "status": "NOK",
        "message": "metadata duplicated into iRODS"
      },
      {
        "date": "2010-01-07T22:11:55.949Z",
        "xml": "xml_samplesheet_2017_01_07_run_09.xml",
        "status": "NOK",
        "message": "metadata was not validated by XSD schema"
      },
      {
        "date": "2010-01-07T19:55:99.999Z",
        "xml": "xml_samplesheet_2017_01_07_run_09.xml",
        "status": "OKKK",
        "message": "metadata loaded into iRODS successfullyyyyy"
      }
    ]
  }
}

Use jq-play to dry-run your jq-filter and optimize any way you want.

2 of 4
79

Rather than using |=, consider using +=:

.data.messages += [{"date": "2010-01-07T19:55:99.999Z",
   "xml": "xml_samplesheet_2017_01_07_run_09.xml",
   "status": "OKKK", "message": "metadata loaded into iRODS successfullyyyyy"}]

Prepend

On the other hand, if (as @NicHuang asked) you want to add the JSON object to the beginning of the array, you could use the pattern:

 .data.messages |= [ _ ] + .
🌐
Earthly
earthly.dev › blog › jq-select
JQ Select Explained: Selecting elements from JSON with Examples - Earthly Blog
July 24, 2023 - This syntax is the same syntax for creating an object in a JSON document. The only difference is you can use the object and array queries you’ve built up as the values. Returning to my GitHub API problem, to wrap the number and the title up into an array I use the object constructor like this: $ curl https://api.github.com/repos/stedolan/jq/issues?per_page=2 | \ jq '[ .[] | { title: .title, number: .number} ]'
🌐
GitHub
gist.github.com › joar › 776b7d176196592ed5d8
Add a field to an object with JQ · GitHub
$ echo '{"hello": "world"}' | jq --arg foo bar '. + {foo: ("not" + $foo)}' { "hello": "world", "foo": "notbar" } Copy link · Copy Markdown · I have json which is an object containing a property that is an array, and I want to create another property with the same value as an existing property, without changing anything else.
🌐
iO Flood
ioflood.com › blog › jq-array
Manipulating JSON Arrays with jq | Example Guide
November 15, 2023 - This guide will walk you through the ins and outs of working with arrays in jq, from basic operations to advanced techniques. We’ll cover everything from the creation, access, and modification of arrays to more complex operations like filtering, mapping, and reducing.
🌐
Zendesk Developer Docs
developer.zendesk.com › documentation › integration-services › developer-guide › jq-cheat-sheet
jq cheat sheet | Zendesk Developer Docs
The following expression extracts ... expr parameter, escape any double quotes with a leading backslash (\"). ... To create an array, wrap the output in square brackets ([])....
🌐
Reddit
reddit.com › r/commandline › jq: multiple objects in arrays
r/commandline on Reddit: jq: multiple objects in arrays
October 10, 2024 -

I've got a jsonl

{"mainobject1": "maindata1", "array1": [{"object1": "data1"}, {"object2": "data2"}]

{"mainobject1": "maindata2", "array1": [{"object1": "data1"}, {"object3": "data3"}]

What I'm doing:

jq -r '[.mainobject1, .array1[].object1, .array1[].object2, .array1[].object3] | @ csv(nospace here)' file.json

What I want to get

maindata1,data1,data2,data3(null)

maindata2,data1,data2(null),data3

What I'm getting

maindata1,data1,null,null,data2,null,null

maindata2,data1,null,null,null,null,data3

Any ideas how to resolve this?