简体   繁体   中英

Extracting a key/value pair from json array using Jolt

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?

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 https://jolt-demo.appspot.com/ is:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM