1
votes

I have a Json like this

    {
      "id" : "1234",
      "name" : "something",
      "list" : [
                {
                 "A" : "Something"
                },
                {
                 "B" : "Something1"
                }
               ]
    }

What I would like is to do is add id and name to both the JSON's present inside list I've gone through a couple of questions and I couldn't find anywhere where somebody had done this.

2

2 Answers

4
votes

I believe the following Shift spec will work:

{
    "id|name": "&",
    "list": {
      "*": {
        "@(2,id)": "&2.[&1].id",
        "@(2,name)": "&2.[&1].name",
        "*": "&2.[&1].&"
      }
    }
}

With your sample data, the output produced was:

{
    "id": "1234",
    "name": "something",
    "list": [{
        "id": "1234",
        "name": "something",
        "A": "Something"
    }, {
        "id": "1234",
        "name": "something",
        "B": "Something1"
    }]
}
1
votes

This spec should give you what you want:

[
  {
    "operation": "shift",
    "spec": {
      "list": {
        "*": {
          "@(2,id)": "&2.[&1].id",
          "@(2,name)": "&2.[&1].name",
          "*": "&2.[&1].&"
        }
      }
    }
  }
]

With your input it gives the following as output:

{
  "list" : [ {
    "A" : "Something",
    "id" : "1234",
    "name" : "something"
  }, {
    "B" : "Something1",
    "id" : "1234",
    "name" : "something"
  } ]
}