Jolt变换未命名的数组

时间:2016-08-31 22:26:17

标签: java json jolt

使用来自jsonplaceholder.com的示例json响应,我想对返回的未命名数组执行Jolt转换。

然而,使用jolt demo我只能在命名数组后转换数组(在本例中为“records”),并用花括号包装。像这样:

Json输入:

{
"records": [
    {
        "id": 1,
        "name": "Leanne Graham",
        "username": "Bret",
        "email": "Sincere@april.biz",
        "address": {
            "street": "Kulas Light",
            "suite": "Apt. 556",
            "city": "Gwenborough",
            "zipcode": "92998-3874",
            "geo": {
                "lat": "-37.3159",
                "lng": "81.1496"
            }
        },
        "phone": "1-770-736-8031 x56442",
        "website": "hildegard.org",
        "company": {
            "name": "Romaguera-Crona",
            "catchPhrase": "Multi-layered client-server neural-net",
            "bs": "harness real-time e-markets"
        }
    },
    {
        "id": 2,
        "name": "Ervin Howell",
        "username": "Antonette",
        "email": "Shanna@melissa.tv",
        "address": {
            "street": "Victor Plains",
            "suite": "Suite 879",
            "city": "Wisokyburgh",
            "zipcode": "90566-7771",
            "geo": {
                "lat": "-43.9509",
                "lng": "-34.4618"
            }
        },
        "phone": "010-692-6593 x09125",
        "website": "anastasia.net",
        "company": {
            "name": "Deckow-Crist",
            "catchPhrase": "Proactive didactic contingency",
            "bs": "synergize scalable supply-chains"
        }
    }
  ]
}

Jolt Spec:

[ { "operation": "shift", "spec": { "records": { "*": { "id": "records[&1].user-id", "username": "records[&1].user-username", "email": "records[&1].user-email", "address": { "street": "records[&2].user-street", "suite": "records[&2].user-suite", "city": "records[&2].user-city", "zipcode": "records[&2].user-zipcode" } } } } } ]

我的示例的目标是展平响应中返回的对象层次结构,同时保持基本的[{}, {}, ...]结构。

当输入是未命名的json数组时,如何实现这一点?

2 个答案:

答案 0 :(得分:1)

我刚发布后才意识到答案......

以下是我对任何感兴趣的人的修订规范:

[ { "operation": "shift", "spec": { "*": { "id": "[&1].user-id", "username": "[&1].user-username", "email": "[&1].user-email", "address": { "street": "[&2].user-street", "suite": "[&2].user-suite", "city": "[&2].user-city", "zipcode": "[&2].user-zipcode" } } } } ]

在声明规范("spec": { "*": { ")后简单匹配,然后使用[&1][&2]等访问未命名的数组

答案 1 :(得分:1)

下面有一个可以工作,但不会变平

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "id": "[&1].user-id",
          "username": "[&1].user-username",
          "email": "[&1].user-email",
          "address": {
            "street": "[&2].user-street",
            "suite": "[&2].user-suite",
            "city": "[&2].user-city",
            "zipcode": "[&2].user-zipcode"
          }
        }
      }
    }
  }
]
相关问题