The user with no hat
The user with no hat

Reputation: 10846

How to append a json object from command line?

I have a json file and I need to read it and write back an additional value (string) to the root of the object. Is there any way to do that from the command line? I've found an utility named JQ but it's a bit unclear how we can write the values back. Here is a sample of the object JSON object. The json tree is unimportant as I'm interested only to inject the string only into the root of the json object.

{"widget": {
    "debug": "on",
    "window": {
        "height": 500
    },
    "text": {
        "data": "Click Here",
        "size": 36,
        "style": "bold",
        "name": "text1",
        "hOffset": 250,
        "vOffset": 100,
        "alignment": "center",
        "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
    }
}}    

The json written back should look like this:

{
  "Category": "Unknown",
  "widget": {
    "debug": "on",
    "window": {
      "height": 500
    },
    "text": {
      "data": "Click Here",
      "size": 36,
      "style": "bold",
      "name": "text1",
      "hOffset": 250,
      "vOffset": 100,
      "alignment": "center",
      "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
    }
  }
}

Upvotes: 0

Views: 1778

Answers (1)

Jeff Mercado
Jeff Mercado

Reputation: 134841

Just set the new field directly using simple assignment.

.Category = "Unknown"

Upvotes: 1

Related Questions