震动转换 JSON 规范

huangapple go评论74阅读模式
英文:

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           
    }
}]
英文:

I need to transform below Input JSON to output JSON and not sure about how to write spec for that. Need to re-position one field ("homePage") as a root element. Any help or suggestion would be appreciated.

Input JSON :

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

Output JSON :

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

答案1

得分: 3

This Jolt Spec should work for you. Tested with 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"
        }
      }
    }
  }
]

input:

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

output:

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

Explanation:

From the 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
      }
   }
}

Next thing: How to wrap the output object into the array?
A good example can be found in this post.

So, in our case:

  • "&[&1].uuid" says:

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.

  • Next, [&2] is similar to [&1]. However, looking at the "indexable" key, it is one level deeper in the input json. Thats why instead of [&1] we used [&2] (have a look again at the foo-bar example from the docs).
英文:

This Jolt Spec should work for you. Tested with 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"
        }
      }
    }
  }
]

input:

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

output:

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

Explanation:

From the 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
> }
> }
> }
>

Next thing: How to wrap the output object into the array?
A good example can be found in this post.

So, in our case:

  • "[&1].uuid" says:
    > 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.
  • Next, [&2] is similar to [&1]. However, looking at the "indexable" key, it is one level deeper in the input json. Thats why instead of [&1] we used [&2] (have a look again at the foo-bar example from the docs).

huangapple
  • 本文由 发表于 2020年8月25日 22:52:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63581550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定