Update JSON File with Bash Using NodeJS

02-22-2022

In this (very) short post I’ll show you a quick way to make simple edits to a .json file from the command line or using bash and NodeJS.

Let’s say we want to update a value in a data.json file. With NodeJS installed on our system we can leverage the -p or --print command to evaluate a script and print the result. For us, the script is the JSON.stringify() function that will evaluate our .json file and add or update certain values in the statement.

Example:

1
node -p "JSON.stringify({...require('./data.json'), key4: 'value4'}, null, 2)" > data.json

Inspired by this post: Source