我有第三方服务,它返回以下回复:
JSON 1
{"Bag":{"Type":{"$":"LIST"},"Source":{"$":"ABC"},"Id":{"$":"151559458"},"Name":{"$":"Bag list"},"Source":{"$":"ABC"},"CustomerId":{"$":"abc@gmail.com"},"DateTime":{"$":"2014-07-17T12:36:01Z"}}}
但我必须将此JSON格式化为以下格式:
JSON2
{"Bag":{"Type":"LIST","Source":"ABC","Id":"151559458","Name":"Bag list","Source":"ABC","CustomerId":"abc@gmail.com","DateTime":"2014-07-17T12:36:01Z"}}
反之亦然,从客户端我得到JSON2,我必须以JSON1格式将此响应发送给服务。
答案 0 :(得分:0)
答案 1 :(得分:0)
您可以使用jq工具http://stedolan.github.io/jq
然后转换是控制台一行:
$ jq '{Bag: .Bag | with_entries({key, value: .value."$"})}' file.json
结果是
{
"Bag": {
"Type": "LIST",
"Source": "ABC",
"Name": "Bag\nlist",
"Id": "151559458",
"DateTime": "2014-07-17T12:36:01Z",
"CustomerId": "abc@gmail.com"
}
}