简体   繁体   English

使用 Jolt 从 json 数组中提取键/值对

[英]Extracting a key/value pair from json array using Jolt

currently I have the following input json.目前我有以下输入json。 I want to extract the object with value attrs.name = Details and append it to the output json (Outside the attrs array). 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. 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:输入 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 attrs.name = "Details" 可以在 attrs 中以任何顺序排列

Desired Output所需 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当前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?有没有办法删除仍然在 attrs 数组中的 attrs.name = Details object?

You need conditional logic among name = Details and the others such as您需要name = Detailsothers之间的条件逻辑,例如

[
  {
    "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: https://jolt-demo.appspot.com/网站上的演示是:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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