Home > Enterprise >  Extracting a key/value pair from json array using Jolt
Extracting a key/value pair from json array using Jolt

Time:09-09

currently I have the following input json. I want to extract the object with value attrs.name = Details and append it to the output json (Outside the attrs array). Currently, while I'm able to append it in the output JSON, I'm still getting a copy of the object inside attrs. I would like this copy to be removed.

Input JSON :

{
  "description": "Data",
  "number": "2022",
  "version": 1,
  "attrs": [
    {
      "name": "Type",
      "value": "DataPack"
    },
    {
      "name": "customerName",
      "value": "ABC"
    },
    {
      "name": "Details",
      "value": {
        "createdDate": "2020-09-10",
        "description": "Value Pack",
        "id": "20020",
        "state": "Complete",
        "requestedCompletionDate": "2022-09-13"
      }
    }
  ]
}

The attrs.name = "Details" can be at any order within attrs

Desired Output

{
    "description": "Data",
    "number": "2022",
    "version": 1,
    "attrs": [
        {
            "name": "Type",
            "value": "DataPack"
        },
        {
            "name": "customerName",
            "value": "ABC"
        }
    ],
    "Details": {
        "createdDate": "2020-09-10",
        "description": "Value Pack",
        "id": "20020",
        "state": "Complete",
        "requestedCompletionDate": "2022-09-13"
    }
}

Current Output

{
  "description": "Data",
  "number": "2022",
  "version": 1,
  "attrs": [
    {
      "name": "Type",
      "value": "DataPack"
    },
    {
      "name": "customerName",
      "value": "ABC"
    },
    {
      "name": "Details",
      "value": {
        "createdDate": "2020-09-10",
        "description": "Value Pack",
        "id": "20020",
        "state": "Complete",
        "requestedCompletionDate": "2022-09-13"
      }
    }
  ],
  "Details": {
    "createdDate": "2020-09-10",
    "description": "Value Pack",
    "id": "20020",
    "state": "Complete",
    "requestedCompletionDate": "2022-09-13"
  }
}

Jolt Spec Used

[    
  {
    "operation": "shift",
    "spec": {
      "attrs": {
        "*": {
          "name": {
            "Details": {
              "@(2,value)": "Details"
            }
          },
          "value": "attrs[#2].value",
          "@name": "attrs[#2].name"
        }
      },
      "*": "&"
    }
  }
]

Is there a way to remove the attrs.name = Details object that's still coming in attrs array?

CodePudding user response:

You need conditional logic among name = Details and the others such as

[
  {
    "operation": "shift",
    "spec": {
      "*": "&", // else case (the attributes other than "attrs" array)
      "attrs": {
        "*": {
          "name": {
            "*": { "@2": "&4" }, // &4 replicates "attrs" (by going up the tree 4 levels)
            "Details": {
              "@(2,value)": "&1" // &1 replicates "Details" (by going up the tree 1 level)
            }
          }
        }
      }
    }
  }
]

the demo on the site enter image description here

  • Related