JOLT规范-将数组转置为类

时间:2019-09-02 14:39:55

标签: json jolt

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

我还可以通过另一个JOLT规范运行OUTPUT,然后检索我需要的JSON输出,但是我仍然不知道JOLT规范应该是什么样子,我看过示例并且没有更深入地了解上面提到的。

输入->变换->输出


这是INPUT 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 SPEC

[
  {
    "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"
}]

1 个答案:

答案 0 :(得分:0)

您非常亲密。我所做的-将名称和deviceId放入t对象,在另一个规范中,我删除了该对象的名称。

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