Writing a jq to add key names on a json list
I was trying to write a query in linux for alternate a json file and write it in a new file.
My json file is formatted like so:
{"intents": [
{
"patterns": "'For the last 8 years of his life, Galileo was under house arrest for espousing this man's theory'",
"responses": "Copernicus"
},
{
"patterns": "'No. 2: 1912 Olympian; football star at Carlisle Indian School; 6 MLB seasons with the Reds, Giants & Braves'",
"responses": "Jim Thorpe"
},
for about 200k entries like the above.
The command that I execute is bellow:
jq --argjson r
"$( jo tag = "$(curl -Ss "https://www.random.org/integers/?num=1&min=0&max=1000000&col=1&base=10&format=plain&rnd=new")")"
'.intents[] += $r'
< intents7.json > intents_super.json
i was wanted to add a new keyname in the list as a tag name and i wanted to fill the key(the tag) for every entry with a random number.
The command was executed but i am waiting like 30 mins so far and nothing is outputting in the file intents_super.json.
Note: The cpu is on constant 100%
also in terminal i was getting those 2 lines, the command still running though..:
Argument `tag' is neither k=v nor k@v
Argument `17208' is neither k=v nor k@v
Does the command do what I want?
I fixed my problem by another way:
od -t x -An /dev/random | tr -d " " | fold -w 8 | jq -nRc --slurpfile stream intents7.json '$stream[] .intents[] | .tag=input' > intensfinalllllll.json
Assuming you want to add a new key, tag
, to each of the elements of the top-level intents
array with a random number as value, then you may do this like so:
some-command | jq -n 'input | .intents |= map(.tag = input)' intents7.json -
… where some-command
is a command that generates an endless stream of random numbers, one per line (for example, shuf -i 0-100 -r
or jot -r 0
or similar).
The jq
command is used with its -n
option to turn off the normal reading of the input. Instead, input
is used to fetch the next object.
The first object is read from the file intents7.json
, and it contains the intents
array that we want to modify. We modify each element of the array by using map()
. The expression that we map
onto each element adds a tag
key with the value read using input
. Each call to input
(after the very initial one) reads from the standard input stream of jq
. On that stream, there are random numbers available, and these are used for the values of the new tag
keys.
Example run (using a corrected variant of the data in the question):
$ shuf -i 0-1000 -r | jq -n 'input | .intents |= map(.tag = input)' intents7.json -
{
"intents": [
{
"patterns": "'For the last 8 years of his life, Galileo was under house arrest for espousing this man's theory'",
"responses": "Copernicus",
"tag": 517
},
{
"patterns": "'No. 2: 1912 Olympian; football star at Carlisle Indian School; 6 MLB seasons with the Reds, Giants & Braves'",
"responses": "Jim Thorpe",
"tag": 955
}
]
}