使用jolt转换空场

时间:2016-09-22 02:28:06

标签: jolt

我的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。我需要添加任何内容吗?

1 个答案:

答案 0 :(得分: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": ""
    }
  }
]