使用 Jolt 从 JSON 输出中删除空值

Posted

技术标签:

【中文标题】使用 Jolt 从 JSON 输出中删除空值【英文标题】:Remove null values from JSON output using Jolt 【发布时间】: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"
    
  ]

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

【问题讨论】:

【参考方案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,很有帮助

以上是关于使用 Jolt 从 JSON 输出中删除空值的主要内容,如果未能解决你的问题,请参考以下文章

从 Greenplum 中的 JSON 中删除 NULL 字段

如何在 android 中使用 Gson 或 VOLley 从 json 中删除空值

如何从json输入中删除空值

JSONObject 删除空值对

熊猫在 to_json 时删除空值

如何从 NSDictionary 中删除空值