英文:
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 lookupAs 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. Foruuid
it will be the index of the array, where the object withuuid
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 theuuid
value in the object inside the array. The index of the array is indicated by the&1
wildcard. Foruuid
it will be the index of the array, where the object withuuid
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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论