【问题标题】:using jolt to transform empty field使用 jolt 转换空字段
【发布时间】:2016-09-22 02:28:05
【问题描述】:

我的 json 看起来像这样

{  
    "group":{  
        "personnel":[  
            {  
                "member":{  
                    "id":"1",
                    "name":"John"
                }
            },
            {  
                "member":{  
                    "id":"2",
                    "name":"Doe"
                }
            }
        ]
    }
}

预期的输出是

[  
    {  
        "id":"1",
        "name":"John"
    },
    {  
        "id":"2",
        "name":"Doe"
    }
]

但是,有时 json 也会像这样为空:

{}

为此,我希望输出为

[]

我的规格看起来像这样

"spec":{  
    "group":{  
        "personnel":{  
            "*":{  
                "*":"[]"
            }
        }
    }
}

但这不适用于json为空的第二种情况,它只会返回null。我需要添加什么吗?

【问题讨论】:

    标签: jolt


    【解决方案1】:

    有点笨拙,但这很管用。

    规格

    [
      {
        "operation": "shift",
        "spec": {
          "group": {
            "personnel": {
              "*": {
                // write to a temp array so that we can 
                //  default it into existence later if needed
                "*": "temp[]"
              }
            }
          }
        }
      },
      {
        "operation": "default",
        "spec": {
          // If "temp" does not exist, make it be an empty array
          "temp": []
        }
      },
      {
        "operation": "shift",
        "spec": {
          // write value at "temp" to the root level
          "temp": ""
        }
      }
    ]
    

    【讨论】:

      猜你喜欢
      • 2021-10-05
      • 2022-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多