英文:
Jolt Transformation: Un-nest object of form "key":{"value":"xyz"} to "key":"value"
问题
以下是你要翻译的内容:
Desired output
[
{
"key1": "abc",
"key2": "xyz"
},
{
"key1": "123",
"key2": "456"
}
]
英文:
I need to unwrap JSON objects in order to reduce the nesting of input like
[
{
"key1": {
"value": "abc"
},
"key2": {
"value": "xyz"
}
},
{
"key1": {
"value": "123"
},
"key2": {
"value": "456"
}
}
]
Instead it can just map straight to the value without the unnecessary object nesting.
Input JSON
[
{
"typedValues": {
"key1": {
"value": "abc"
},
"key2": {
"value": "xyz"
}
}
},
{
"typedValues": {
"key1": {
"value": "123"
},
"key2": {
"value": "456"
}
}
}
]
My spec attempt
I was able to remove the "typedValues" wrapping but unable to achieve the key:value reduction.
[
{
"operation": "shift",
"spec": {
"*": {
"typedValues": {
"@": ""
}
}
}
}
]
Output from attempt
[
{
"key1": {
"value": "abc"
},
"key2": {
"value": "xyz"
}
},
{
"key1": {
"value": "123"
},
"key2": {
"value": "456"
}
}
]
Desired output
[
{
"key1": "abc",
"key2": "xyz"
},
{
"key1": "123",
"key2": "456"
}
]
答案1
得分: 0
您可以使用shift变换,例如:
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"value": "&3.&1" // &3: 上溯树3级以获取包装对象的最外层索引 // &1: 上溯1级后复制键1/2
}
}
}
}
},
{ // 用于去除对象键
"operation": "shift",
"spec": {
"*": ""
}
}
]
或者使用@value
与&
通配符进行快速匹配,该通配符表示键值的当前级别复制:
[
{
"operation": "shift",
"spec": {
"*": {
"*": { // typedValues
"*": {
"@value": "[&3].&"
}
}
}
}
}
]
英文:
You can use a shift transformaion such as
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"value": "&3.&1" // &3 : going 3 levels up the tree to grab the outermost indexes of the wrapper objects
} // &1 : replicating key 1/2 after going 1 level up
}
}
}
},
{ // to get rid of the object keys
"operation": "shift",
"spec": {
"*": ""
}
}
]
or shorly match @value
with &
wildcard which represents the current level replication of the key values
[
{
"operation": "shift",
"spec": {
"*": {
"*": { //typedValues
"*": {
"@value": "[&3].&"
}
}
}
}
}
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论