JOLT 规范 - 将数组转置为类



我需要使用 JOLT 规范来转换 JSON 结构。 我使用 https://jolt-demo.appspot.com 在下面测试以下内容。

我也可以通过另一个 JOLT 规范运行 OUTPUT,然后检索我需要的 JSON 输出,但我仍然不知道 JOLT 规范应该是什么样子,我已经看过示例,并没有更接近上面提到的内容。

输入 -> 转换 -> 输出


这是输入 JSON

{
"result": 0,
"companys": [
{
"id": 3,
"pId": 322,
"nm": "Balfin Trading (PTY) LTD"
},
{
"id": 25,
"pId": 0,
"nm": "FootCam"
}
],
"vehicles": [
{
"id": 487,
"desc": null,
"ic": 2,
"nm": "GCS001",
"pnm": null,
"dl": [
{
"id": "60335",
"ic": 0,
"us": null,
"md": 361,
"tn": "",
"vt": null,
"io": "",
"isb": null,
"sim": "0726168807",
"cc": 4,
"dId": null,
"sdc": null,
"pid": 83,
"st": null,
"tc": 0,
"cn": "ROAD,CAB,R SIDE VIEW,L SIDE VIEW",
"nflt": null
}
],
"adt": null,
"pvg": null,
"djb": null,
"etm": null,
"pid": 83,
"phone": null,
"abbr": null,
"vtp": null,
"st": null,
"dt": null,
"dn": null,
"linesOperation": null
},
{
"id": 486,
"desc": null,
"ic": 2,
"nm": "GCS002",
"pnm": null,
"dl": [
{
"id": "60334",
"ic": 0,
"us": null,
"md": 361,
"tn": "",
"vt": null,
"io": "",
"isb": null,
"sim": "0767024106",
"cc": 4,
"dId": null,
"sdc": null,
"pid": 83,
"st": null,
"tc": 0,
"cn": "ROAD,CAB,R SIDE,L SIDE",
"nflt": null
}
],
"adt": null,
"pvg": null,
"djb": null,
"etm": null,
"pid": 83,
"phone": null,
"abbr": null,
"vtp": null,
"st": null,
"dt": null,
"dn": null,
"linesOperation": null
},
{
"id": 491,
"desc": null,
"ic": 2,
"nm": "GCS003",
"pnm": null,
"dl": [
{
"id": "60294",
"ic": 0,
"us": null,
"md": 361,
"tn": "",
"vt": null,
"io": "",
"isb": null,
"sim": "0795732047",
"cc": 4,
"dId": null,
"sdc": null,
"pid": 83,
"st": null,
"tc": 0,
"cn": "ROAD,CAB,R SIDE,L SIDE",
"nflt": null
}
],
"adt": null,
"pvg": null,
"djb": null,
"etm": null,
"pid": 83,
"phone": null,
"abbr": null,
"vtp": null,
"st": null,
"dt": null,
"dn": null,
"linesOperation": null
}
]
}

这是我目前拥有的 JOLT 规格

[
{
"operation": "shift",
"spec": {
"vehicles": {
"*": {
"nm": "name",
"dl": {
"*": {
"id": "deviceID"
}
}
}
}
}
}
]

给出以下输出

{
"name" : [ "GCS001", "GCS002", "GCS003" ],
"deviceID" : [ "60335", "60334", "60294" ]
}

相反,我要求输出是

[{
"name" : "GCS001", 
"deviceID" : "60335"
},
{
"name" : "GCS002", 
"deviceID" : "60334"
},
{
"name" : "GCGCS003S001", 
"deviceID" : "60294"
}]

你非常接近。我做了什么 - 将名称和设备ID放入t对象,并在另一个规范中删除了该对象的名称。

[
{
"operation": "shift",
"spec": {
"vehicles": {
"*": {
"nm": "t[&1].name",
"dl": {
"*": {
"id": "t[&3].deviceID"
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"t": ""
}
}
]
[
{
"operation": "shift",
"spec": {
"vehicles" : {
"*" : {
"@(0,nm)" : "[&1].name",
"dl" : {
"*" : {
"@(0,id)" : "[&3].deviceID"
}
}
}
}
}
}
]

最新更新