Skip to main content
1 of 6
kyles
  • 33
  • 5

How can I add a property to json in the end of the object (text manipulation)

I'm doing some text manipulation and I want to add a property to a json object (at the end)

e.g

object = {
  "one": "one",
  "two": "two",
}

Should become

object = {
  "one": "one",
  "two": "two",
  "three": "three",
}

This is my attempt, but i'm wondering if there is a cleaner way of doing things.

#find line for }   
line=$(grep -n "$(awk "/object/{f=1;next} /}/{f=0} f" file.txt | tail -n 1)" file.txt | awk -F : '{ print $1 }')
content='\"three\" = \"three\"'
awk 'NR=='$((line + 1))' {print "\t '"$content"'"} 1' file.txt > file.temp && mv file.temp file.txt)
kyles
  • 33
  • 5