【问题标题】:Remove null values from JSON output using Jolt使用 Jolt 从 JSON 输出中删除空值
【发布时间】:2022-01-19 18:35:36
【问题描述】:

您能否建议我一种从 json 输出中删除 null 值的方法,如下所述:

输入

{
  "userId": "1",
  "age": "20",
  "desc1": "value desc1",
  "desc2": "value desc2",
  "desc3": "value desc3",
  "desc4": "value desc4",
  "desc5": "value desc5",
  "desc6": "value desc6",
  "desc7": "value desc7"
}

规格

[
  {
    "operation": "shift",
    "spec": {
      "desc4": "test4",
      "desc5": "test5",
      "desc6": "test6",
      "desc1|desc2|desc3": {
        "$": "additionalInformationList[#2].typeCode",
        "@": "additionalInformationList[#2].value"
      }
    }
  }
]

输出

{
  "test4": "value desc4",
  "test5": "value desc5",
  "test6": "value desc6",
  "additionalInformationList": [null,null,null,
    {
      "typeCode": "desc1",
      "value": "value desc1"
    },
    {
      "typeCode": "desc2",
      "value": "value desc2"
    },
    {
      "typeCode": "desc3",
      "value": "value desc3"
    }
  ]
}

有什么建议如何删除空值吗?

【问题讨论】:

    标签: java json jolt


    【解决方案1】:

    确实够用了

    [
      {
        "operation": "shift",
        "spec": {
          "desc*": "&",
          "desc1|desc2|desc3": {
            "$": "additionalInformationList[#2].typeCode",
            "@": "additionalInformationList[#2].value"
          }
        }
      },
      {
        "operation": "remove",
        "spec": {
          "desc7": ""
        }
      }
    ]
    

    如果您不使用 test 重命名 desc 属性以获取

    {
      "additionalInformationList" : [ {
        "typeCode" : "desc1",
        "value" : "value desc1"
      }, {
        "typeCode" : "desc2",
        "value" : "value desc2"
      }, {
        "typeCode" : "desc3",
        "value" : "value desc3"
      } ],
      "desc4" : "value desc4",
      "desc5" : "value desc5",
      "desc6" : "value desc6"
    }
    

    对于目前的情况,可以使用

    [
      {
        "operation": "shift",
        "spec": {
          "desc*": "test&",
          "desc1|desc2|desc3": {
            "$": "additionalInformationList[#2].typeCode",
            "@": "additionalInformationList[#2].value"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "test*": {
            "$": "@(0)"
          },
          "*": "&"
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*desc*": "=split('desc',@(1,&))"
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*desc*": "=join('',@(1,&))"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*desc*": {
            "$": "@(0)"
          },
          "*": "&"
        }
      },
      {
        "operation": "remove",
        "spec": {
          "test7": ""
        }
      }
    ]
    

    通过单独编写 desc4desc5desc6 属性的键值对来缩短大小写可能是

    [
      {
        "operation": "shift",
        "spec": {
          "desc4": "test4",
          "desc5": "test5",
          "desc6": "test6",
          "desc*": {
            "$": "&.code",
            "@": "&.value"
          }
        }
      },
      {
        "operation": "remove",
        "spec": {
          "desc7": ""
        }
      },
      {
        "operation": "shift",
        "spec": {
          "test*": "&",
          "*": "additionalInformation[]"
        }
      }
    ]
    

    【讨论】:

    • 非常感谢@Barbaros,很有帮助
    猜你喜欢
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2019-02-04
    • 1970-01-01
    • 2015-03-07
    • 2013-09-06
    • 2012-11-23
    相关资源
    最近更新 更多