Add an object to existing JSON using jq
# Optional: Create new JSON file `feed.json` with empy array.
jq -n '[]' > feed.json
# Append an object to the array from `feed.json`
# and store the new JSON in `feed.json.tmp`
jq \
--arg date "$date" \
--arg title "$title" \
'. += [{"date": $date, "title": $title}]' \
feed.json > feed.json.tmp
# Replace temp file with original file.
mv feed.json.tmp feed.json
--arg content "$content"creates a variable$contentto be used within thejqtool.'. += [{...}]' feed.jsonappends a new object to the array fromfeed.json.> feed.json.tmpis redirecting the output ofjqinto a temporarily file.mv feed.json.tmp feed.jsonis replacing original file with the new temporarily file. Basically updating the original file with the new content.
No reactions yet