请帮助我回答下面的问题. 我有一个基于条件使用Jolt转换JSON的需求. 下面是我的输入JSON.

{
  "body": {
    "Activity": [
      {
        "attributeCode": "ABC",
        "attributeValue": "1200"
      },
      {
        "attributeCode": "DEF",
        "attributeValue": "1400"
      },
      {
        "attributeCode": "GHI",
        "attributeValue": "1600"
      }
    ],
    "NonActivity": {
      "bill": "23Oct11",
      "activityNumber": "6100"
    }
  },
  "header": {
    "id": "010400",
    "referenceNumber": "6100"
  },
  "action": "CREATE"
}

如果操作不是CREATE,那么我想完全删除"Activity[]",如下所示:

{
  "body": {
    "NonActivity": {
      "bill": "23Oct11",
      "activityNumber": "6100"
    }
  },
  "header": {
    "id": "010400",
    "referenceNumber": "6100"
  },
  "action": "CREATE"
}

到目前为止,下面是我try 的内容,但问题是,当没有创建操作时,我得到的是一个空:


[
  {
    "operation": "shift",
    "spec": {
      "action": {
        "CREATE": {
          "@2": ""
        },
        "*": {
          "@(2,body.Activity[&])": ""
        }
      }
    }
  }
]

推荐答案

您可以使用以下转换规范:

[
  {
    "operation": "shift",
    "spec": {
      "body": {
        "@1,action": {
          "CREATE": { // if outer "action" == "CREATE"
            "@2": "&3" // @2 -- > the whole value taken from the 2 levels above(eg. the value of the "body")
                       // &3 -- > the label taken from 3 levels above( eg. "body" )   
          },
          "*": { // if outer "action" != "CREATE"
            "*|@2,NonActivity": "&3.NonActivity"
          }
        }
      },
      "*": "&" // the elements at the top level other than "body"
    }
  }
]

考虑到您可能具有"活动"和"非活动"之外的元素,最好使用以下元素:

[
  {
    "operation": "shift",
    "spec": {
      "body": {
        "Activity": "&1.&.@(2,action)", // assign a label by action's value nested within "Activity" object
        "*": "&1.&"
      },
      "*": "&"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "body": {
        "Activity": { "CREATE": "&2.&1" },
        "*": "&1.&"
      },
      "*": "&"
    }
  }
]

In case there is no field like "action": "CREATE" in the input, is there a way to tell JOLT to leave the input as is

可以,但您可以通过添加修改默认规范并重新安排,如下所示来告诉Jolt这样做:

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "action": "ReMoVe"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "body": {
        "Activity": "&1.&.@(2,action)", 
        "*": "&1.&"
      },
      "*": "&"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "body": {
        "Activity": { "CREATE": "&2.&1" },
        "*": "&1.&"
      },
      "*": "&",
      "action": { "ReMoVe": { "": "" }, "*": { "@1": "&2" } }
    }
  }
]

Json相关问答推荐

Allof Indide的JSON模式之一

当有2个嵌套数组时展平复杂的JSON

Pandas 对REST API的自定义响应

如何将文件夹组织的.json文件合并为一个JSON文件,文件夹/文件名作为键

使用 Powershell,如何将 Azure AD 组成员转换为 Json 对象(文件),然后可以更新?

XSLT 3.0 Json-to-xml,json 包含 html struct

删除 JOLT 中的方括号

将 json 转换为 jsonb 安全吗?

如何在 jq 中按 IP 地址排序?

JOLT JSON 将值从一对多转换为一对一

如何使用 jq 返回此 JSON 文件的文本字段?

C# 合并 2 个几乎相同的 JSON 对象

将 js Array() 转换为 JSON 对象以用于 JQuery .ajax

避免 KeyError 的默认字典键

获取json中某个键的索引

为什么在我们有 json_encode 时使用 CJSON 编码

Rails 中奇怪的 JSON Javascript 问题

在 Rails 3 中处理 JS/ERB 模板中的 JSON

Golang struct 的 XML 和 JSON 标签?

如何从 jQuery ajax 调用将复杂对象传递给 ASP.NET WebApi GET?