JQ iterate in shell, line by line

Sometimes you want to work item by item in a JSON array

Published: Tuesday, Oct 6, 2020 Last modified: Thursday, Nov 14, 2024

#!/bin/bash
cat << EOF |
[ { "foo": 42, "something": "else" }, { "foo": 12, "more": { "age": 12} } ]
EOF
jq -c -r '.[]' | while read -r json
do 
	jq -r '.foo' <<< $json | while read -r foo
	do
		addTen=$(( "$foo" + 10))
		jq --arg addTen "$addTen" '.properties.addTen = ($addTen|tonumber)' <<< $json
	done
done | jq -s