Search code examples
jsonapache-nifijolt

How to insert json object in nested json using Jolt Spec


I have a use case, where I needs to put all JSON objects in nested JSON array. I have tried to get this using @ but getting null all the time. posted the jolt which i'am using.

Input :

{
  "status": "Pink",
  "summary": "violate",
  "type": "Image",
  "affectedPic": [
    {
      "PicType": "Nature",
      "name": "County-nature",
      "PicId": 4239
    },
    {
      "PicType": "Abstract",
      "name": "Buildings",
      "PicId": 1937
    },
    {
      "PicType": "Technology",
      "name": "AI",
      "PicId": 6937
    }
  ],
  "archived": true
}

Expected Output :


{
  "affectedPic": [
    {
      "PicType": "Nature",
      "name": "County-nature",
      "PicId": 4239,
      "status": "Pink",
      "summary": "violate",
      "type": "Image",
      "archived": true
    },
    {
      "PicType": "Abstract",
      "name": "Buildings",
      "PicId": 1937,
      "status": "Pink",
      "summary": "violate",
      "type": "Image",
      "archived": true
    },
    {
      "PicType": "Technology",
      "name": "AI",
      "PicId": 6937,
      "status": "Pink",
      "summary": "violate",
      "type": "Image",
      "archived": true
    }
  ]
}

My Jolt :

[
  {
    "operation": "shift",
    "spec": {
      "status": "@(1,affectedPic)",
      "summary": "@(1,affectedPic)",
      "type": "@(1,affectedPic)",
      "archived": "@(1,affectedPic)"
    }
  }
]

I have applied multiple combinations in JOLT Spec but it didn't works, Please suggest.


Solution

  • You can use

    [
      {
        "operation": "shift",
        "spec": {
          "affectedPic": {
            "*": {
              "@(2,status)": "[#2].status", // go two levels up to grab the value of 
                                            // the attribute at the level of 
                                            // the "affectedPic" array
                                            // generate array-wise results by [#2] after 
                                            // reaching the indexes level of the array
                                            // to start counting with 1 instead of 0  
              "@(2,summary)": "[#2].summary",
              "@(2,type)": "[#2].type",
              "*": "[#2].&",
              "@(2,archived)": "[#2].archived"
            }
          }
        }
      }
    ]
    

    the demo on the site http://jolt-demo.appspot.com/ is :

    enter image description here

    or more dynamically (without individually writing the attributes those stay outside of the array) :

    [
      {
        "operation": "shift",
        "spec": {
          "*": "others.&",
          "affectedPic": "&"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "affectedPic": {
            "*": {
              "@2,others": { "*": "[&1].&" },
              "*": "[&1].&" // generate array-wise results by [&1] to 
                                            // reach the indexes level of the array
                                            // to start counting with 0(as 0,1)
            }
          }
        }
      }
    ]
    

    the demo on the site http://jolt-demo.appspot.com/ is :

    enter image description here