i've a json file like this
[{"id": 25422},{"id": 25423}]
and i like to update the id value and obtain this result
[{"id": "coupon25422"},{"id": "coupon25423"}]
Is this possible? thank you
Yes it is.
$ jq -c 'map(.id |= "coupon\(.)")' <<< '[{"id": 25422},{"id": 25423}]'
[{"id":"coupon25422"},{"id":"coupon25423"}]
Related
Objective:
I want to filter json file by specific key name and to get only its value as output
filter to be used: id (in below json)
What I tried:
cat /tmp/output.json | jq -r 'values'
{
"name": "abotcontext7",
"id": "20436855-4635-49c0-8cc9-a389c569233b",
"created_at": "2022-06-02T10:39:48.280Z"
}
Output should be just: 20436855-4635-49c0-8cc9-a389c569233b
I tried cat /tmp/output.json|jq -r '.id[]
Its not working. Any suggestions please
As per #pmf suggestion it worked
jq -r 'values.id' /tmp/output.json
I have the id command and I want to store the output of the id command in the JSON file like
{
id : uid=0(root) gid=0(root)
}
How to do that
Use jq to generate JSON
jq --arg id "$(id)" --null-input '$ARGS.named' > file.json
This could be the simplest answer:
echo "{ \"id\": \"$(id)\" }" > file.json
But your question does not offer any specific info..so I am just guessing how you are trying t pull the info, how or where you need it...etc.
I have a javascript file which prints a JSON array of objects:
// myfile.js output
[
{ "id": 1, "name": "blah blah", ... },
{ "id": 2, "name": "xxx", ... },
...
]
In my bash script, I want to iterate through each object.
I've tried following, but it doesn't work.
#!/bin/bash
output=$(myfile.js)
for row in $(echo ${output} | jq -c '.[]'); do
echo $row
done
You are trying to invoke myfile.js as a command. You need this:
output=$(cat myfile.js)
instead of this:
output=$(myfile.js)
But even then, your current approach isn't going to work well if the data has whitespace in it (which it does, based on the sample you posted). I suggest the following alternative:
jq -c '.[]' < myfile.js |
while read -r row
do
echo "$row"
done
Output:
{"id":1,"name":"blah blah"}
{"id":2,"name":"xxx"}
Edit:
If your data is arising from a previous process invocation, such as mongo in your case, you can pipe it directly to jq (to remain portable), like this:
mongo myfile.js |
jq -c '.[]' |
while read -r row
do
echo "$row"
done
How can I make jq -c '.[]' < (mongo myfile.js) work?
In a bash shell, you would write an expression along the following lines:
while read -r line ; do .... done < <(mongo myfile.js | jq -c .[])
Note that there are two occurrences of "<" in the above expression.
Also, the above assumes mongo is emitting valid JSON. If it emits //-style comments, those would have somehow to be removed.
Comparison with piping into while
If you use the idiom:
... | while read -r line ; do .... done
then the bindings of any variables in .... will be lost.
I have a json file which has the below data
{"Item": {"ID": {"S": "4869949"},"no":{"N": "2"}}}
I need to retrieve the S value from ID and N value from no and put them into a query as parameters.. the query looks like this:
query --table-name validation \
--key-condition-expression "ID = :v1 AND no = :v2" \
--expression-attribute-values '{":v1": {"S": "${ID}"},":v2": {"N": "${no}"}}' \
--region us-east-1
I have tried using jq but couldn't figure out how to read a json file and retrieve the values as parameters. This is the first time I am using jq. Can someone help with it?
The following may not be exactly what you're after but does show how you can avoid calling jq more than once, and how the command can partly be constructed by jq. In other words, assuming the templates for --key-condition-expression and --expression-attribute-values are fixed, you should be able to adapt the following to your needs:
Assuming a bash or bash-like shell, and that data.json holds:
{"Item": {"ID": {"S": "4869949"},"no":{"N": "2"}}}
we could write:
ID=ID
no=no
< data.json jq --arg ID "$ID" --arg no "$no" -cr '
.Item
| .ID.S as $v1
| .no.N as $v2
| $v1, $v2, {($v1): {"S": $ID}, ($v2): {"N": $no}}
' | while read -r v1
do
read -r v2
read -r query
echo query --table-name validation \
--key-condition-expression "\"ID = $v1 AND no = $v2\"" \
--expression-attribute-values "'""$query""'" \
--region us-east-1
done
jq as a template engine
If the template with :v1 and :v2 in the Q are variable, then you could adapt the above by using jq as a template engine, a topic which is covered in the jq Cookbook: https://github.com/stedolan/jq/wiki/Cookbook#using-jq-as-a-template-engine
It's basic jq syntax:
$ ID=$(cat data.json | jq .Item.ID.S -r)
$ no=$(cat data.json | jq .Item.no.N -r)
$ echo $ID $no
4869949 2
I'd suggest you read something like https://shapeshed.com/jq-json/ to get more familiar though.
I have json like this:
[ {"one": 1}, {"two": 2}]
and wish to convert it to this format:
{"one": 1}
{"two": 2}
to facilitate indexing it into ElasticSearch. (latter is called 'jsonl' format). JQ is my tool of preference but I can't figure out how to do this.
Thanks
The key is the -c command-line option, which produces JSONL:
jq -c '.[]' test_array.json
figured this out:
cat test_array.json |jq '.[]'