change json file by bash script










37















I need your help to solve the following problem:
I have a JSON file that looks like this:




"key1": "value1",
"key2": "value2",
"key3": "value3"



how can I add and remove a new key (i.e "key4": "value4") by bash script?
I see also the issue to add or remove a comma at the end of last key in the file before adding or removing the new one.



Thank you










share|improve this question
























  • Does it have to be bash or can you use Node.js? This seems like a great problem to use node for.

    – Patrick Gunderson
    Jul 24 '14 at 20:34















37















I need your help to solve the following problem:
I have a JSON file that looks like this:




"key1": "value1",
"key2": "value2",
"key3": "value3"



how can I add and remove a new key (i.e "key4": "value4") by bash script?
I see also the issue to add or remove a comma at the end of last key in the file before adding or removing the new one.



Thank you










share|improve this question
























  • Does it have to be bash or can you use Node.js? This seems like a great problem to use node for.

    – Patrick Gunderson
    Jul 24 '14 at 20:34













37












37








37


20






I need your help to solve the following problem:
I have a JSON file that looks like this:




"key1": "value1",
"key2": "value2",
"key3": "value3"



how can I add and remove a new key (i.e "key4": "value4") by bash script?
I see also the issue to add or remove a comma at the end of last key in the file before adding or removing the new one.



Thank you










share|improve this question
















I need your help to solve the following problem:
I have a JSON file that looks like this:




"key1": "value1",
"key2": "value2",
"key3": "value3"



how can I add and remove a new key (i.e "key4": "value4") by bash script?
I see also the issue to add or remove a comma at the end of last key in the file before adding or removing the new one.



Thank you







json bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 24 '14 at 21:07









mklement0

128k20241270




128k20241270










asked Jul 24 '14 at 19:58









user3155074user3155074

2281311




2281311












  • Does it have to be bash or can you use Node.js? This seems like a great problem to use node for.

    – Patrick Gunderson
    Jul 24 '14 at 20:34

















  • Does it have to be bash or can you use Node.js? This seems like a great problem to use node for.

    – Patrick Gunderson
    Jul 24 '14 at 20:34
















Does it have to be bash or can you use Node.js? This seems like a great problem to use node for.

– Patrick Gunderson
Jul 24 '14 at 20:34





Does it have to be bash or can you use Node.js? This seems like a great problem to use node for.

– Patrick Gunderson
Jul 24 '14 at 20:34












2 Answers
2






active

oldest

votes


















68














Your best bet is to use a JSON CLI such as jq:



  • On Debian-based systems such as Ubuntu, you can install it via sudo apt-get install jq

  • On macOS, with Homebrew (http://brew.sh/) installed, use brew install jq

Examples, based on the following input string - output is to stdout:



jsonStr=' "key1": "value1", "key2": "value2", "key3": "value3" '


Remove "key3":



jq 'del(.key3)' <<<"$jsonStr"


Add property "key4" with value "value4":



jq '. + "key4": "value4" ' <<<"$jsonStr"


Change the value of existing property "key1" to "new-value1":



jq '.key1 = "new-value1"' <<<"$jsonStr"


A more robust alternative thanks, Lars Kiesow
:

If you pass the new value with --arg, jq takes care of properly escaping the value:



jq '.key1 = $newVal' --arg newVal '3 " of rain' <<<"$jsonStr"



If you want to update a JSON file in place (conceptually speaking), using the example of deleting "key3":



# Create test file.
echo ' "key1": "value1", "key2": "value2", "key3": "value3" ' > test.json

# Remove "key3" and write results back to test.json (recreate it with result).
jq -c 'del(.key3)' test.json > tmp.$$.json && mv tmp.$$.json test.json


You cannot replace the input file directly, so the result is written to a temporary file that replaces the input file on success.



Note the -c option, which produces compact rather than pretty-printed JSON.



For all options and commands, see the manual at http://stedolan.github.io/jq/manual/.






share|improve this answer




















  • 3





    I really wish Apple included this project in macOS by default...

    – Sonic84
    Oct 25 '17 at 15:57


















18














Not the answer for everyone, but if you already happen to have NodeJs installed in your system, you can use it to easily manipulate JSON.



eg:



#!/usr/bin/env bash
jsonFile=$1;

node > out_$jsonFile <<EOF
//Read data
var data = require('./$jsonFile');

//Manipulate data
delete data.key3
data.key4 = 'new value!';

//Output data
console.log(JSON.stringify(data));

EOF


Heck, if you only need to do JSON manipulation and you have node (ie: You don't really need any other bash functionality) you could directly write a script using node as the interpreter:



#! /usr/bin/env node
var data = require('./'+ process.argv[2]);
/*manipulate*/
console.log(JSON.stringify(data));





share|improve this answer























  • Is the console.log() supposed to write the data to the file or do I need fs?

    – Shawn Mclean
    Aug 11 '17 at 3:24






  • 1





    prints to stdout. You can then redirect to a file. eg: ./myScript.js myInput.json > outfile.txt

    – Lenny Markus
    Aug 12 '17 at 4:59












  • any programming language with a JSON library could do the exact same. The main point of using bash is that you don't rely on software that isn't already installed on your system. If that is not a requirement, then there's no reason to suggest Node for this over any other language, and just because you can run it inline from a bash script doesnt make it a bash answer.

    – user5359531
    Aug 22 '18 at 14:52












  • I started my answer with "Not the answer for everyone," for that precise reason. There are many ways to skin a cat, and in the three years since I posted this answer, Node has continued to grow in popularity, so many people have it installed already. It offers native JSON support, so no need for a library is nice. At the end of the day, I shared a bit of knowledge, and it has helped 16 people. That makes me happy :)

    – Lenny Markus
    Aug 28 '18 at 22:11











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24942875%2fchange-json-file-by-bash-script%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









68














Your best bet is to use a JSON CLI such as jq:



  • On Debian-based systems such as Ubuntu, you can install it via sudo apt-get install jq

  • On macOS, with Homebrew (http://brew.sh/) installed, use brew install jq

Examples, based on the following input string - output is to stdout:



jsonStr=' "key1": "value1", "key2": "value2", "key3": "value3" '


Remove "key3":



jq 'del(.key3)' <<<"$jsonStr"


Add property "key4" with value "value4":



jq '. + "key4": "value4" ' <<<"$jsonStr"


Change the value of existing property "key1" to "new-value1":



jq '.key1 = "new-value1"' <<<"$jsonStr"


A more robust alternative thanks, Lars Kiesow
:

If you pass the new value with --arg, jq takes care of properly escaping the value:



jq '.key1 = $newVal' --arg newVal '3 " of rain' <<<"$jsonStr"



If you want to update a JSON file in place (conceptually speaking), using the example of deleting "key3":



# Create test file.
echo ' "key1": "value1", "key2": "value2", "key3": "value3" ' > test.json

# Remove "key3" and write results back to test.json (recreate it with result).
jq -c 'del(.key3)' test.json > tmp.$$.json && mv tmp.$$.json test.json


You cannot replace the input file directly, so the result is written to a temporary file that replaces the input file on success.



Note the -c option, which produces compact rather than pretty-printed JSON.



For all options and commands, see the manual at http://stedolan.github.io/jq/manual/.






share|improve this answer




















  • 3





    I really wish Apple included this project in macOS by default...

    – Sonic84
    Oct 25 '17 at 15:57















68














Your best bet is to use a JSON CLI such as jq:



  • On Debian-based systems such as Ubuntu, you can install it via sudo apt-get install jq

  • On macOS, with Homebrew (http://brew.sh/) installed, use brew install jq

Examples, based on the following input string - output is to stdout:



jsonStr=' "key1": "value1", "key2": "value2", "key3": "value3" '


Remove "key3":



jq 'del(.key3)' <<<"$jsonStr"


Add property "key4" with value "value4":



jq '. + "key4": "value4" ' <<<"$jsonStr"


Change the value of existing property "key1" to "new-value1":



jq '.key1 = "new-value1"' <<<"$jsonStr"


A more robust alternative thanks, Lars Kiesow
:

If you pass the new value with --arg, jq takes care of properly escaping the value:



jq '.key1 = $newVal' --arg newVal '3 " of rain' <<<"$jsonStr"



If you want to update a JSON file in place (conceptually speaking), using the example of deleting "key3":



# Create test file.
echo ' "key1": "value1", "key2": "value2", "key3": "value3" ' > test.json

# Remove "key3" and write results back to test.json (recreate it with result).
jq -c 'del(.key3)' test.json > tmp.$$.json && mv tmp.$$.json test.json


You cannot replace the input file directly, so the result is written to a temporary file that replaces the input file on success.



Note the -c option, which produces compact rather than pretty-printed JSON.



For all options and commands, see the manual at http://stedolan.github.io/jq/manual/.






share|improve this answer




















  • 3





    I really wish Apple included this project in macOS by default...

    – Sonic84
    Oct 25 '17 at 15:57













68












68








68







Your best bet is to use a JSON CLI such as jq:



  • On Debian-based systems such as Ubuntu, you can install it via sudo apt-get install jq

  • On macOS, with Homebrew (http://brew.sh/) installed, use brew install jq

Examples, based on the following input string - output is to stdout:



jsonStr=' "key1": "value1", "key2": "value2", "key3": "value3" '


Remove "key3":



jq 'del(.key3)' <<<"$jsonStr"


Add property "key4" with value "value4":



jq '. + "key4": "value4" ' <<<"$jsonStr"


Change the value of existing property "key1" to "new-value1":



jq '.key1 = "new-value1"' <<<"$jsonStr"


A more robust alternative thanks, Lars Kiesow
:

If you pass the new value with --arg, jq takes care of properly escaping the value:



jq '.key1 = $newVal' --arg newVal '3 " of rain' <<<"$jsonStr"



If you want to update a JSON file in place (conceptually speaking), using the example of deleting "key3":



# Create test file.
echo ' "key1": "value1", "key2": "value2", "key3": "value3" ' > test.json

# Remove "key3" and write results back to test.json (recreate it with result).
jq -c 'del(.key3)' test.json > tmp.$$.json && mv tmp.$$.json test.json


You cannot replace the input file directly, so the result is written to a temporary file that replaces the input file on success.



Note the -c option, which produces compact rather than pretty-printed JSON.



For all options and commands, see the manual at http://stedolan.github.io/jq/manual/.






share|improve this answer















Your best bet is to use a JSON CLI such as jq:



  • On Debian-based systems such as Ubuntu, you can install it via sudo apt-get install jq

  • On macOS, with Homebrew (http://brew.sh/) installed, use brew install jq

Examples, based on the following input string - output is to stdout:



jsonStr=' "key1": "value1", "key2": "value2", "key3": "value3" '


Remove "key3":



jq 'del(.key3)' <<<"$jsonStr"


Add property "key4" with value "value4":



jq '. + "key4": "value4" ' <<<"$jsonStr"


Change the value of existing property "key1" to "new-value1":



jq '.key1 = "new-value1"' <<<"$jsonStr"


A more robust alternative thanks, Lars Kiesow
:

If you pass the new value with --arg, jq takes care of properly escaping the value:



jq '.key1 = $newVal' --arg newVal '3 " of rain' <<<"$jsonStr"



If you want to update a JSON file in place (conceptually speaking), using the example of deleting "key3":



# Create test file.
echo ' "key1": "value1", "key2": "value2", "key3": "value3" ' > test.json

# Remove "key3" and write results back to test.json (recreate it with result).
jq -c 'del(.key3)' test.json > tmp.$$.json && mv tmp.$$.json test.json


You cannot replace the input file directly, so the result is written to a temporary file that replaces the input file on success.



Note the -c option, which produces compact rather than pretty-printed JSON.



For all options and commands, see the manual at http://stedolan.github.io/jq/manual/.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jul 25 '18 at 14:42

























answered Jul 24 '14 at 20:28









mklement0mklement0

128k20241270




128k20241270







  • 3





    I really wish Apple included this project in macOS by default...

    – Sonic84
    Oct 25 '17 at 15:57












  • 3





    I really wish Apple included this project in macOS by default...

    – Sonic84
    Oct 25 '17 at 15:57







3




3





I really wish Apple included this project in macOS by default...

– Sonic84
Oct 25 '17 at 15:57





I really wish Apple included this project in macOS by default...

– Sonic84
Oct 25 '17 at 15:57













18














Not the answer for everyone, but if you already happen to have NodeJs installed in your system, you can use it to easily manipulate JSON.



eg:



#!/usr/bin/env bash
jsonFile=$1;

node > out_$jsonFile <<EOF
//Read data
var data = require('./$jsonFile');

//Manipulate data
delete data.key3
data.key4 = 'new value!';

//Output data
console.log(JSON.stringify(data));

EOF


Heck, if you only need to do JSON manipulation and you have node (ie: You don't really need any other bash functionality) you could directly write a script using node as the interpreter:



#! /usr/bin/env node
var data = require('./'+ process.argv[2]);
/*manipulate*/
console.log(JSON.stringify(data));





share|improve this answer























  • Is the console.log() supposed to write the data to the file or do I need fs?

    – Shawn Mclean
    Aug 11 '17 at 3:24






  • 1





    prints to stdout. You can then redirect to a file. eg: ./myScript.js myInput.json > outfile.txt

    – Lenny Markus
    Aug 12 '17 at 4:59












  • any programming language with a JSON library could do the exact same. The main point of using bash is that you don't rely on software that isn't already installed on your system. If that is not a requirement, then there's no reason to suggest Node for this over any other language, and just because you can run it inline from a bash script doesnt make it a bash answer.

    – user5359531
    Aug 22 '18 at 14:52












  • I started my answer with "Not the answer for everyone," for that precise reason. There are many ways to skin a cat, and in the three years since I posted this answer, Node has continued to grow in popularity, so many people have it installed already. It offers native JSON support, so no need for a library is nice. At the end of the day, I shared a bit of knowledge, and it has helped 16 people. That makes me happy :)

    – Lenny Markus
    Aug 28 '18 at 22:11
















18














Not the answer for everyone, but if you already happen to have NodeJs installed in your system, you can use it to easily manipulate JSON.



eg:



#!/usr/bin/env bash
jsonFile=$1;

node > out_$jsonFile <<EOF
//Read data
var data = require('./$jsonFile');

//Manipulate data
delete data.key3
data.key4 = 'new value!';

//Output data
console.log(JSON.stringify(data));

EOF


Heck, if you only need to do JSON manipulation and you have node (ie: You don't really need any other bash functionality) you could directly write a script using node as the interpreter:



#! /usr/bin/env node
var data = require('./'+ process.argv[2]);
/*manipulate*/
console.log(JSON.stringify(data));





share|improve this answer























  • Is the console.log() supposed to write the data to the file or do I need fs?

    – Shawn Mclean
    Aug 11 '17 at 3:24






  • 1





    prints to stdout. You can then redirect to a file. eg: ./myScript.js myInput.json > outfile.txt

    – Lenny Markus
    Aug 12 '17 at 4:59












  • any programming language with a JSON library could do the exact same. The main point of using bash is that you don't rely on software that isn't already installed on your system. If that is not a requirement, then there's no reason to suggest Node for this over any other language, and just because you can run it inline from a bash script doesnt make it a bash answer.

    – user5359531
    Aug 22 '18 at 14:52












  • I started my answer with "Not the answer for everyone," for that precise reason. There are many ways to skin a cat, and in the three years since I posted this answer, Node has continued to grow in popularity, so many people have it installed already. It offers native JSON support, so no need for a library is nice. At the end of the day, I shared a bit of knowledge, and it has helped 16 people. That makes me happy :)

    – Lenny Markus
    Aug 28 '18 at 22:11














18












18








18







Not the answer for everyone, but if you already happen to have NodeJs installed in your system, you can use it to easily manipulate JSON.



eg:



#!/usr/bin/env bash
jsonFile=$1;

node > out_$jsonFile <<EOF
//Read data
var data = require('./$jsonFile');

//Manipulate data
delete data.key3
data.key4 = 'new value!';

//Output data
console.log(JSON.stringify(data));

EOF


Heck, if you only need to do JSON manipulation and you have node (ie: You don't really need any other bash functionality) you could directly write a script using node as the interpreter:



#! /usr/bin/env node
var data = require('./'+ process.argv[2]);
/*manipulate*/
console.log(JSON.stringify(data));





share|improve this answer













Not the answer for everyone, but if you already happen to have NodeJs installed in your system, you can use it to easily manipulate JSON.



eg:



#!/usr/bin/env bash
jsonFile=$1;

node > out_$jsonFile <<EOF
//Read data
var data = require('./$jsonFile');

//Manipulate data
delete data.key3
data.key4 = 'new value!';

//Output data
console.log(JSON.stringify(data));

EOF


Heck, if you only need to do JSON manipulation and you have node (ie: You don't really need any other bash functionality) you could directly write a script using node as the interpreter:



#! /usr/bin/env node
var data = require('./'+ process.argv[2]);
/*manipulate*/
console.log(JSON.stringify(data));






share|improve this answer












share|improve this answer



share|improve this answer










answered Sep 17 '15 at 21:20









Lenny MarkusLenny Markus

2,64021830




2,64021830












  • Is the console.log() supposed to write the data to the file or do I need fs?

    – Shawn Mclean
    Aug 11 '17 at 3:24






  • 1





    prints to stdout. You can then redirect to a file. eg: ./myScript.js myInput.json > outfile.txt

    – Lenny Markus
    Aug 12 '17 at 4:59












  • any programming language with a JSON library could do the exact same. The main point of using bash is that you don't rely on software that isn't already installed on your system. If that is not a requirement, then there's no reason to suggest Node for this over any other language, and just because you can run it inline from a bash script doesnt make it a bash answer.

    – user5359531
    Aug 22 '18 at 14:52












  • I started my answer with "Not the answer for everyone," for that precise reason. There are many ways to skin a cat, and in the three years since I posted this answer, Node has continued to grow in popularity, so many people have it installed already. It offers native JSON support, so no need for a library is nice. At the end of the day, I shared a bit of knowledge, and it has helped 16 people. That makes me happy :)

    – Lenny Markus
    Aug 28 '18 at 22:11


















  • Is the console.log() supposed to write the data to the file or do I need fs?

    – Shawn Mclean
    Aug 11 '17 at 3:24






  • 1





    prints to stdout. You can then redirect to a file. eg: ./myScript.js myInput.json > outfile.txt

    – Lenny Markus
    Aug 12 '17 at 4:59












  • any programming language with a JSON library could do the exact same. The main point of using bash is that you don't rely on software that isn't already installed on your system. If that is not a requirement, then there's no reason to suggest Node for this over any other language, and just because you can run it inline from a bash script doesnt make it a bash answer.

    – user5359531
    Aug 22 '18 at 14:52












  • I started my answer with "Not the answer for everyone," for that precise reason. There are many ways to skin a cat, and in the three years since I posted this answer, Node has continued to grow in popularity, so many people have it installed already. It offers native JSON support, so no need for a library is nice. At the end of the day, I shared a bit of knowledge, and it has helped 16 people. That makes me happy :)

    – Lenny Markus
    Aug 28 '18 at 22:11

















Is the console.log() supposed to write the data to the file or do I need fs?

– Shawn Mclean
Aug 11 '17 at 3:24





Is the console.log() supposed to write the data to the file or do I need fs?

– Shawn Mclean
Aug 11 '17 at 3:24




1




1





prints to stdout. You can then redirect to a file. eg: ./myScript.js myInput.json > outfile.txt

– Lenny Markus
Aug 12 '17 at 4:59






prints to stdout. You can then redirect to a file. eg: ./myScript.js myInput.json > outfile.txt

– Lenny Markus
Aug 12 '17 at 4:59














any programming language with a JSON library could do the exact same. The main point of using bash is that you don't rely on software that isn't already installed on your system. If that is not a requirement, then there's no reason to suggest Node for this over any other language, and just because you can run it inline from a bash script doesnt make it a bash answer.

– user5359531
Aug 22 '18 at 14:52






any programming language with a JSON library could do the exact same. The main point of using bash is that you don't rely on software that isn't already installed on your system. If that is not a requirement, then there's no reason to suggest Node for this over any other language, and just because you can run it inline from a bash script doesnt make it a bash answer.

– user5359531
Aug 22 '18 at 14:52














I started my answer with "Not the answer for everyone," for that precise reason. There are many ways to skin a cat, and in the three years since I posted this answer, Node has continued to grow in popularity, so many people have it installed already. It offers native JSON support, so no need for a library is nice. At the end of the day, I shared a bit of knowledge, and it has helped 16 people. That makes me happy :)

– Lenny Markus
Aug 28 '18 at 22:11






I started my answer with "Not the answer for everyone," for that precise reason. There are many ways to skin a cat, and in the three years since I posted this answer, Node has continued to grow in popularity, so many people have it installed already. It offers native JSON support, so no need for a library is nice. At the end of the day, I shared a bit of knowledge, and it has helped 16 people. That makes me happy :)

– Lenny Markus
Aug 28 '18 at 22:11


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24942875%2fchange-json-file-by-bash-script%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20