Search code examples
jsonapache-nifijolt

Get outer json key value into very json in the nested json array using JOLT


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.


Solution

  • 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"
        }]
    }