颠簸变换 JSON 规范

Jolt Transform JSON Spec

我需要在输入 JSON 下转换为输出 JSON,但不确定如何为此编写规范。需要将一个字段(“homePage”)重新定位为根元素。如有任何帮助或建议,我们将不胜感激。

输入JSON:

[{
    "uuid": "cac40601-ffc9-4fd0-c5a1-772ac65f0587",
    "pageId": 123456,    
    "page": {
      "indexable": true,
      "rootLevel": false,
      "homePage": false      
    }
}]

输出JSON:

[{
    "uuid": "cac40601-ffc9-4fd0-c5a1-772ac65f0587",
    "pageId": 123456,
    "homePage": false,  
    "page": {
      "indexable": true,
      "rootLevel": false           
    }
}]

此 Jolt 规格应该适合您。用 https://jolt-demo.appspot.com/

测试
[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "uuid": "[&1].uuid",
        "pageId": "[&1].pageId",
        "page": {
          "indexable": "[&2].page.indexable",
          "rootLevel": "[&2].page.rootLevel",
          "homePage": "[&2].homePage"
        }
      }
    }
  }
]

输入:

{
  "uuid" : "cac40601-ffc9-4fd0-c5a1-772ac65f0587",
  "pageId" : 123456,
  "page" : {
    "indexable" : true,
    "rootLevel" : false
  },
  "homePage" : false
}

输出:

[ {
  "uuid" : "cac40601-ffc9-4fd0-c5a1-772ac65f0587",
  "pageId" : 123456,
  "page" : {
    "indexable" : true,
    "rootLevel" : false
  },
  "homePage" : false
} ]

解释:

来自javadoc

& Path lookup

As Shiftr processes data and walks down the spec, it maintains a data structure describing the path it has walked. The & wildcard can access data from that path in a 0 major, upward oriented way. Example:

{
   "foo" : {
       "bar": {
         "baz":  // &0 = baz, &1 = bar, &2 = foo
      }
   }
}

下一件事:如何将输出对象包装到数组中? 在 .

中可以找到一个很好的例子

因此,在我们的例子中:

  • "[&1].uuid" 说:

Place the uuid value in the object inside the array. The index of the array is indicated by the &1 wildcard. For uuid it will be the index of the array, where the object with uuid key is placed in the original json.

  • 其次,[&2][&1]类似。但是,查看"indexable"键,它在输入json中更深一层。这就是为什么我们使用 [&2] 而不是 [&1](再次查看文档中的 foo-bar 示例)。