0

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

但是,使用jolt 演示,我只能在命名数组(本例中为“记录”)并用花括号括起来后转换数组。像这样:

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

颠簸规格:

[ { "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 数组时,我该如何实现?

4

2 回答 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"
          }
        }
      }
    }
  }
]
于 2018-05-20T11:32:17.517 回答
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 ( ) 后简单地匹配,然后用,等"spec": { "*": { "访问未命名的数组[&1][&2]

于 2016-08-31T22:31:54.580 回答