我们如何将提供的输入转换为所需的输出?
转换涉及转义字符串并将属性转换为有效的 JSON 格式。但是,“attributes”键中的任何嵌套属性(例如“attr11”)应保留为字符串,而不是转换为 JSON。
输入.json
{
"id": 67046492,
"owner": "John Doe",
"version": 1,
"lineItems": [
{
"id": 219,
"attributes": "{}"
},
{
"id": 220,
"attributes": "{\"attr1\":{\"value\":\"7.0\",\"actionCode\":\"ADD\"},\"attr2\":{\"value\":\"gdajdas\",\"actionCode\":\"ADD\"},\"attr3\":{\"value\":\"Y\",\"actionCode\":\"ADD\"},\"attr4\":{\"actionCode\":\"ADD\"},\"attr5\":{\"value\":\"2\",\"actionCode\":\"ADD\"},\"attr6\":{\"value\":\"jjj\",\"actionCode\":\"ADD\"},\"attr7\":{\"value\":\"N\",\"actionCode\":\"ADD\"},\"attr8\":{\"value\":\"jwidhf\",\"actionCode\":\"ADD\"},\"attr9\":{\"value\":\"kncskc\",\"actionCode\":\"ADD\"},\"attr10\":{\"value\":\"2\",\"actionCode\":\"ADD\"},\"attr11\":{\"value\":\"{\\\"key1\\\":\\\"2\\\",\\\"key2\\\":\\\"7.0\\\",\\\"key3\\\":\\\"3.5\\\",\\\"key4\\\":\\\"\\\"}\",\"actionCode\":\"ADD\"},\"attr12\":{\"value\":\"3.5\",\"actionCode\":\"ADD\"}}"
},
{
"id": 221,
"attributes": "{\"attr1\":{\"value\":\"7.0\",\"actionCode\":\"ADD\"},\"attr2\":{\"value\":\"gdajdas\",\"actionCode\":\"ADD\"},\"attr3\":{\"value\":\"Y\",\"actionCode\":\"ADD\"},\"attr4\":{\"actionCode\":\"ADD\"},\"attr5\":{\"value\":\"2\",\"actionCode\":\"ADD\"},\"attr6\":{\"value\":\"jjj\",\"actionCode\":\"ADD\"},\"attr7\":{\"value\":\"N\",\"actionCode\":\"ADD\"},\"attr8\":{\"value\":\"jwidhf\",\"actionCode\":\"ADD\"},\"attr9\":{\"value\":\"kncskc\",\"actionCode\":\"ADD\"},\"attr10\":{\"value\":\"2\",\"actionCode\":\"ADD\"},\"attr11\":{\"value\":\"{\\\"key1\\\":\\\"2\\\",\\\"key2\\\":\\\"7.0\\\",\\\"key3\\\":\\\"3.5\\\",\\\"key4\\\":\\\"\\\"}\",\"actionCode\":\"ADD\"},\"attr12\":{\"value\":\"3.5\",\"actionCode\":\"ADD\"}}"
}
]
}
预期.json
{
"id": 67046492,
"owner": "John Doe",
"version": 1,
"lineItems": [
{
"id": 219,
"attributes": "{}"
},
{
"id": 220,
"attributes": {
"attr1": {
"value": "7.0",
"actionCode": "ADD"
},
"attr2": {
"value": "gdajdas",
"actionCode": "ADD"
},
"attr3": {
"value": "Y",
"actionCode": "ADD"
},
"attr4": {
"actionCode": "ADD"
},
"attr5": {
"value": "2",
"actionCode": "ADD"
},
"attr6": {
"value": "jjj",
"actionCode": "ADD"
},
"attr7": {
"value": "N",
"actionCode": "ADD"
},
"attr8": {
"value": "jwidhf",
"actionCode": "ADD"
},
"attr9": {
"value": "kncskc",
"actionCode": "ADD"
},
"attr10": {
"value": "2",
"actionCode": "ADD"
},
"attr11": {
"value": "{\"key1\":\"2\",\"key2\":\"7.0\",\"key3\":\"3.5\",\"key4\":\"\"}",
"actionCode": "ADD"
},
"attr12": {
"value": "3.5",
"actionCode": "ADD"
}
}
},
{
"id": 221,
"attributes": {
"attr1": {
"value": "7.0",
"actionCode": "ADD"
},
"attr2": {
"value": "gdajdas",
"actionCode": "ADD"
},
"attr3": {
"value": "Y",
"actionCode": "ADD"
},
"attr4": {
"actionCode": "ADD"
},
"attr5": {
"value": "2",
"actionCode": "ADD"
},
"attr6": {
"value": "jjj",
"actionCode": "ADD"
},
"attr7": {
"value": "N",
"actionCode": "ADD"
},
"attr8": {
"value": "jwidhf",
"actionCode": "ADD"
},
"attr9": {
"value": "kncskc",
"actionCode": "ADD"
},
"attr10": {
"value": "2",
"actionCode": "ADD"
},
"attr11": {
"value": "{\"key1\":\"2\",\"key2\":\"7.0\",\"key3\":\"3.5\",\"key4\":\"\"}",
"actionCode": "ADD"
},
"attr12": {
"value": "3.5",
"actionCode": "ADD"
}
}
}
]
}
我知道这篇文章有点旧,你可能已经从这篇文章中继续前进,但我刚刚遇到了一种名为 jslt 的新 json 转换语言,如果你使用的是 1.19 及更高版本,它恰好在 ApacheNifi 中有一个名为 JSTLTransformJSON 的处理器。花了一些时间学习这门语言,我发现它实际上可以解决 jolt 无法解决的问题。其中之一是使用 from-json 函数的这个问题,规范将如下所示:
{
"id":.id,
"owner":.owner,
"version":.version,
"lineItems":[for(.lineItems) { "id":.id,"attributes":from-json(.attributes) }]
}
实际上,如果您不关心顺序,规格可以更短:
{
"lineItems":[for(.lineItems) { "id":.id,"attributes":from-json(.attributes) }],
*:.
}
它将给出您正在寻找的内容(您可以尝试这里):
{
"id" : 67046492,
"owner" : "John Doe",
"version" : 1,
"lineItems" : [ {
"id" : 219
}, {
"id" : 220,
"attributes" : {
"attr1" : {
"value" : "7.0",
"actionCode" : "ADD"
},
"attr2" : {
"value" : "gdajdas",
"actionCode" : "ADD"
},
"attr3" : {
"value" : "Y",
"actionCode" : "ADD"
},
"attr4" : {
"actionCode" : "ADD"
},
"attr5" : {
"value" : "2",
"actionCode" : "ADD"
},
"attr6" : {
"value" : "jjj",
"actionCode" : "ADD"
},
"attr7" : {
"value" : "N",
"actionCode" : "ADD"
},
"attr8" : {
"value" : "jwidhf",
"actionCode" : "ADD"
},
"attr9" : {
"value" : "kncskc",
"actionCode" : "ADD"
},
"attr10" : {
"value" : "2",
"actionCode" : "ADD"
},
"attr11" : {
"value" : "{\"key1\":\"2\",\"key2\":\"7.0\",\"key3\":\"3.5\",\"key4\":\"\"}",
"actionCode" : "ADD"
},
"attr12" : {
"value" : "3.5",
"actionCode" : "ADD"
}
}
}, {
"id" : 221,
"attributes" : {
"attr1" : {
"value" : "7.0",
"actionCode" : "ADD"
},
"attr2" : {
"value" : "gdajdas",
"actionCode" : "ADD"
},
"attr3" : {
"value" : "Y",
"actionCode" : "ADD"
},
"attr4" : {
"actionCode" : "ADD"
},
"attr5" : {
"value" : "2",
"actionCode" : "ADD"
},
"attr6" : {
"value" : "jjj",
"actionCode" : "ADD"
},
"attr7" : {
"value" : "N",
"actionCode" : "ADD"
},
"attr8" : {
"value" : "jwidhf",
"actionCode" : "ADD"
},
"attr9" : {
"value" : "kncskc",
"actionCode" : "ADD"
},
"attr10" : {
"value" : "2",
"actionCode" : "ADD"
},
"attr11" : {
"value" : "{\"key1\":\"2\",\"key2\":\"7.0\",\"key3\":\"3.5\",\"key4\":\"\"}",
"actionCode" : "ADD"
},
"attr12" : {
"value" : "3.5",
"actionCode" : "ADD"
}
}
} ]
}
希望这能帮助将来遇到同样问题的人。