29

json file as follows:

{"name" :"sam",
"age":23,
"designation":"doctor"}

now i want to add another json object {"location":"canada"} at the end of the file using bash script i have tried echo "{"location":"canada"}">>sample.json

but it results

{"name" :"sam",
"age":23,
"designation":"doctor"} {location:canada}

but i want it to be like this

{"name" :"sam",
"age":23,
"designation":"doctor", 
"location":"canada"}

please suggest me

user2353439
  • 489
  • 2
  • 7
  • 18
  • Shell script is not very well suited for this task. You should try to find a tool which really understands JSON. If you know Python, have a look at [`json.tool`](http://docs.python.org/2/library/json.html). – tripleee May 30 '13 at 05:14

2 Answers2

68

To merge two json objects, you could use jq command-line utility:

$ jq -s add sample.json another.json

Output:

{
  "name": "sam",
  "age": 23,
  "designation": "doctor",
  "location": "canada"
}

To update a single attribute:

$ jq '.location="canada"' sample.json

It produces the same output.

To prepend "doctor" to the location:

$ jq '.location = "doctor" + .location' input.json

Output:

{
  "name": "sam",
  "age": 23,
  "designation": "doctor",
  "location": "doctorcanada"
}
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • 3
    I've only worked with jq for 5 minutes but it's hella flexible. I would highly recommend this over doing complicated stuff with ed/sed/awk. – Niels Bom May 21 '14 at 09:50
  • 6
    `jq '.location="canada"' sample.json` was the only thing that worked, but it didn't append to sample.json; it just prints it to the screen. The whole idea was we wanted to append to a son file. `jq -s add` keeps giving "parse error: Invalid numeric literal at line 3, column 32". Even if you delete "age":23, you still get "invalid numeric literal". How did this work for you guys? –  Feb 16 '17 at 17:53
  • 2
    @Bear: all examples in the answer work (I've just checked again on the current `jq` version on my machine: `jq --version` -> jq-`1.5-1-a5b5cbe`). All examples print to the screen (you don't want to corrupt your input data while you're adapting the filter for your case. If you don't know how to save the output to a file; ask). I see that someone has upvoted your comment. Does it mean there is an environment where `jq` doesn't work? – jfs Jul 12 '17 at 09:04
  • For your convenience `jq ... INPUT > INPUT.tmp && mv INPUT.tmp INPUT` https://stackoverflow.com/questions/36565295/jq-to-replace-text-directly-on-file-like-sed-i – Sida Zhou Jul 23 '21 at 02:42
18
sed -i '$s/}/,\n"location":"canada"}/' sample.json

Result:

{"name" :"sam",
"age":23,
"designation":"doctor",
"location":"canada"}
perreal
  • 94,503
  • 21
  • 155
  • 181
  • thanks it's working perfectly i have one more requirement i want to append one json object value to another object. for example i want to append "doctor" to canada it should result {"name" :"sam", "age":23, "designation":"doctor", "location":"doctorcanada"} – user2353439 May 30 '13 at 05:10
  • @user2353439: It can be done based on the same principles.Look up the `sed` manpage. This is not rocket science. – kampu May 30 '13 at 05:53
  • 2
    @kampu: using `sed` to manipulate json data with regexes is brittle and unnecessary complicated. [There are better alternatives](http://stackoverflow.com/a/16835501/4279). – jfs May 30 '13 at 12:09
  • @J.F.Sebastian: Upvoted. That's a useful tool, though I think in most cases I'd just do it in Python with the `json` module. Overall I'm happy to give an answer I know to be suboptimal to a clearly lazy questioner -- they should continue to suffer in self imposed ignorance until they get over it and start doing a reasonable level of research :) – kampu May 30 '13 at 12:41
  • @kampu: this question was viewed 2k+ times so far. It doesn't matter whether OP has showed any effort (you can downvote the question) -- other people shouldn't suffer even if you think OP deserves it (personally I would rather ignore a question completely than to provide "suboptimal" answer intentionally. – jfs Apr 25 '15 at 18:05
  • 1
    Just FYI -- If you do `sed -i`... on a Mac, you get this error: "sed: 1: "sample.json": unterminated substitute pattern". But if you do it in Linux (Ubuntu), you're fine. –  Feb 16 '17 at 18:01
  • 1
    Anyone know how to use a variable in that statement? If you do: **loc="canada"**, and then do `sed -i '$s/}/,\n"location":"$loc"}/' sample.json`, it doesn't work. Instead of rendering the variable it puts "location":"$loc". –  Feb 16 '17 at 18:06
  • 2
    `'$s/}/,\n"location":"'$loc'"}/'` – perreal Feb 16 '17 at 18:16