Use slurp mode:

  o   --slurp/-s:

      Instead of running the filter for each JSON object
      in the input, read the entire input stream into a large
      array and run the filter just once.
$ jq -s '.' < tmp.json
[
  {
    "name": "John",
    "email": "[email protected]"
  },
  {
    "name": "Brad",
    "email": "[email protected]"
  }
]
Answer from chepner 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 ... 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

bash - jq - create empty array and add objects to it - Stack Overflow
create empty FINAL array for(eachService is serviceList) a. CURL returning JSON array of objects b. use jq filters to parse JSON response, apply some logic and modify elements in response as needed c. More on stackoverflow.com
🌐 stackoverflow.com
Creating Array of Objects from Bash Array using jq - Stack Overflow
I am trying to create an array of objects in bash given an array in bash using jq. Here is where I am stuck: IDS=("baf3eca8-c4bd-4590-bf1f-9b1515d521ba" "ef2fa922-2038-445c-9d32- More on stackoverflow.com
🌐 stackoverflow.com
json - Create object from array of keys and values - Stack Overflow
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? Inpu... More on stackoverflow.com
🌐 stackoverflow.com
Transform list of objects into an array
I'm looking to transform list of objects like this below into an array of elements, so I can count them with '| length' More on github.com
🌐 github.com
4
September 1, 2015
🌐
Zendesk Developer Docs
developer.zendesk.com › documentation › integration-services › developer-guide › jq-cheat-sheet
jq cheat sheet | Zendesk Developer Docs
Before using this expression in the expr parameter, escape any double quotes with a leading backslash (\"). ... To create an array, wrap the output in square brackets ([]). The following expression combines the collaborator_ids and submitter_id properties into a single array.
🌐
Simon Willison
til.simonwillison.net › jq › array-of-array-to-objects
Turning an array of arrays into objects with jq | Simon Willison’s TILs
May 25, 2021 - {"id":"mm.domus.SW230","name":" A LA RONDE","category":"Buildings:Houses:Medium houses","latitude":50.642781,"longitude":-3.405508} {"id":"mm.domus.SW193","name":" ALEXANDER KEILLER MUSEUM","category":"Archaeology:Prehistory","latitude":51.427927,"longitude":-1.857344} {"id":"mm.domus.SE416","name":" ANNE OF CLEVES HOUSE MUSEUM","category":"Buildings:Houses:Medium houses","latitude":50.869227,"longitude":0.005329} jq Flattening nested JSON objects with jq - 2021-03-11
🌐
Programming Historian
programminghistorian.org › en › lessons › json-and-jq
Reshaping JSON with jq | Programming Historian
May 24, 2016 - Note that we do not have to start this query by breaking apart an array like we did with the Rijskmuseum data. This is because the Twitter data comes in the JSON lines format, with one separate JSON object per line in the file. jq simply repeats the filter for each of these separate objects. This has created a set of JSON objects (wrapped in {}) with an id key and a hashtags key.
🌐
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 - 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.
Find elsewhere
🌐
Qmacro
qmacro.org › blog › posts › 2022 › 05 › 06 › converting-strings-to-objects-with-jq
Converting strings to objects with jq - DJ Adams
May 6, 2022 - Similar to the array construction there's also the object construction, with which objects can be created on the fly quite easily. And as the manual says: If the keys are "identifier-like", then the quotes can be left off · So I can use name rather than "name" for the property, reducing the JSON noise a little: jq -R '[.,inputs] | map(sub("^.+/";"")) | map({name: .})' names.txt
🌐
jq
jqlang.org › manual
jq 1.8 Manual
This is useful when using jq as a simple calculator or to construct JSON data from scratch. ... Don't parse the input as JSON. Instead, each line of text is passed to the filter as a string. If combined with --slurp, then the entire input is passed to the filter as a single long string. ... Instead of running the filter for each JSON object in the input, read the entire input stream into a large array and run the filter just once.
🌐
Exercism
exercism.org › tracks › jq › concepts › arrays
Arrays in jq on Exercism
Ruby has a handy each_with_index method that pairs the element value with its index. Python has enumerate. jq has something similar, named to_entries. This transforms the array to an array of {key: index, value: value} objects.
🌐
iO Flood
ioflood.com › blog › jq-array
Manipulating JSON Arrays with jq | Example Guide
November 15, 2023 - In this example, we’re using the echo command to create an empty array, and then we’re using jq to add an element to it. The '. += ["element"]' part of the command is where the magic happens. This is jq’s syntax for adding an element to an array.
🌐
jq recipes
remysharp.com › drafts › jq-recipes
jq recipes
April 16, 2024 - echo "1\n2\n3" | jq --slurp --raw-input 'split("\n")[:-1]' ... Convert a plain list of timestamps to an array of objects with date and time separated (using jq's --slurp and --raw-input options combined):
🌐
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} ]'
🌐
Cameronnokes
cameronnokes.com › blog › jq-cheatsheet
JQ cheatsheet - Cameron Nokes
August 5, 2020 - echo '{ "a": 1, "b": 2 }' | jq '{ a: .b, b: .a }' # { "a": 2, "b": 1 } Gets an object’s keys as an array. ... Or the number of top level keys.
🌐
GitHub
github.com › jqlang › jq › issues › 933
Transform list of objects into an array · Issue #933 · jqlang/jq
September 1, 2015 - Hi, I'm looking to transform list of objects like this below into an array of elements, so I can count them with '| length' Want to get something like this: [ {object1}, {object2} ] out of this: { "Sid": "somesid", "Effect": "Allow", "Pr...
Author   rokka-n