簡體   English   中英

使用 Jolt 從 json 數組中提取鍵/值對

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

目前我有以下輸入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. 我希望刪除此副本。

輸入 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"
      }
    }
  ]
}

attrs.name = "Details" 可以在 attrs 中以任何順序排列

所需 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"
    }
}

當前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"
  }
}

使用的顛簸規格

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

有沒有辦法刪除仍然在 attrs 數組中的 attrs.name = Details object?

您需要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)
            }
          }
        }
      }
    }
  }
]

https://jolt-demo.appspot.com/網站上的演示是:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM