Home > Mobile >  Edit a .json file (add a new line with Linux command line)
Edit a .json file (add a new line with Linux command line)

Time:06-10

{
"layerOrder": [
    "Layer1",
    "Layer2",
    "Layer3",
    "Layer4"
    ]
}

This is target.json, I need to add a "Layer0" before "Layer1", by Linux command line.

{
"layerOrder": [
    "Layer0",
    "Layer1",
    "Layer2",
    "Layer3",
    "Layer4"
    ]
}

How can I do it?
Thanks in advance!

CodePudding user response:

If you just want to get the job done given what you have stated in the question, then this will work:

$ head json -n 2; echo -e "    \"Layer0\","; sed -n '3,$p' json > newjson

CodePudding user response:

sed -ie 's/"layerOrder": \[/"layerOrder": \[ \n\t\t"Layer0",/' file.json

I found this approach, the main problem was with special symbols ([)

  • Related