使用Jolt转换对象数组

时间:2018-03-23 11:21:51

标签: java transform jolt

我尝试使用JOLT将一个对象数组转换为输出JSON而不使用包装密钥。

INPUT

{
  "emps": [
    {
      "emp": {
        "empId": "2A68",
        "emailAddress": "abc@xyz.com",
        "name": "abc",
        "userId": "82869",
        "userType": "none",
        "phoneNumber": "1234",
        "rank": "2"
      }
    }
  ]
}

SPEC I TRIED

[
  {
    "operation": "shift",
    "spec": {
      "emps": {
        "*": {
          "empId": "data.result[&1].emps[&1].empId",
          "name": "data.result[&1].emps[&1].name",
          "phoneNumber": "data.result[&1].emps[&1].phone",
          "emailAddress": "data.result[&1].emps[&1].email"
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "data": {
        "result[]": {
          "*": {
            "emps[]": []
          }
        }
      }
    }
  }
]

预期输出

{
  "data" : {
    "result" : [ {
      "emps" : [ {
        "empId" : "2A68",
        "name" : "abc",
        "phone" : "1234",
        "email" : "abc@xyz.com"
      } ]
    } ]
  }
}

请复制并粘贴以上INPUTOUTPUT here 如果我从输入中删除emp包装器,那么它的工作正常,但是没有得到如何使用emp包装器获得相同的输出。

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:2)

规格

必须先进行第一次轮班,然后逐步进入" emps",数组,然后进行" emp"对象

[
  {
    "operation": "shift",
    "spec": {
      "emps": {
        "*": {
          "emp": {
            "empId": "data.result[0].emps[&2].empId",
            "name": "data.result[0].emps[&2].name",
            "phoneNumber": "data.result[0].emps[&2].phone",
            "emailAddress": "data.result[0].emps[&2].email"
          }
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "data": {
        "result[]": {
          "*": {
            "emps[]": []
          }
        }
      }
    }
  }
]
相关问题