[英]Broke nested dynamic JSON array with JOLT
I'm looking for flattening nested JSON file into SQL ready format.我正在寻找将嵌套的 JSON 文件展平为 SQL 就绪格式。
JSON file's content: JSON 文件内容:
{
"ProductLine": [
"Product 1",
"Product 2"
],
"Purchase": 364,
"Cancel": [
140,
2
]
}
My current transformation:我目前的转型:
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"@": "[#2].&2"
}
}
}
}
]
Desired output:所需的 output:
[
{
"ProductLine": "Product 1",
"Purchase": 364,
"Cancel": 140
},
{
"ProductLine": "Product 2",
"Cancel": 2
}
]
The difficulty is that arrays can change, sometimes "Cancel" can be an array or sometimes "Purchase" block can be nested.难点在于arrays可以改变,有时“取消”可以是一个数组,有时“购买”块可以嵌套。
You can use this spec:您可以使用此规范:
If Purchase
or cancel
be an array or not, this works如果Purchase
或cancel
是一个数组或不是,这有效
[
{
"operation": "cardinality",
"spec": {
"*": "MANY"
}
},
{
"operation": "shift",
"spec": {
"ProductLine": {
"*": {
"*": {
"@1": "[&2].&3",
"@(3,Purchase[&1])": "[&2].Purchase",
"@(3,Cancel[&1])": "[&2].Cancel"
}
}
}
}
}
]
First, change all values to the array.首先,将所有值更改为数组。 Now you can loop on the ProductLine
and get other fields from Purchase
and Cancel
.现在您可以在ProductLine
上循环并从Purchase
和Cancel
获取其他字段。
Update: The following answer has been obtained in collaboration with Barbaros Özhan .更新:以下答案是与Barbaros Özhan合作获得的。 Special thanks.特别感谢。
[
{
"operation": "cardinality",
"spec": {
"*": "MANY"
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"@": "[#2].&2"
}
}
}
}
]
We can pick Purchase
at a different( outer ) level such as我们可以在不同的(外部)级别选择Purchase
,例如
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"@": "[#2].&2"
}
},
"Purchase": "[#].&"// at two level less than the inner object
}
}
]
the demo one the site http://jolt-demo.appspot.com/ is演示网站http://jolt-demo.appspot.com/是
Edit : Considering array indeterminance for the attributes, you can use the following spec alternatively编辑:考虑到属性的数组不确定性,您可以选择使用以下规范
[
{ //reform two separate objects
"operation": "shift",
"spec": {
"@": "orj",
"*": "non_array.&.@0[]"
}
},
{ // in order to keep the non-array values as the first component of the newly formed array(s)
"operation": "sort"
},
{
"operation": "shift",
"spec": {
"*": { //the topmost level
"*": { //level for the keys
"*": "&1[]" //match keys and values to convert non-arrays to arrays
}
}
}
},
{// pick the first component for the non-array(s)
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": "=firstElement"
}
}
},
{ // apply the original spec after having got individual array values
"operation": "shift",
"spec": {
"*": {
"*": {
"@": "[#2].&2"
}
}
}
},
{ //get rid of the attributes with null values
"operation": "modify-overwrite-beta",
"spec": {
"*": "=recursivelySquashNulls"
}
}
]
or another straightforward alternative would be using your original spec after applying cardinality spec such as或者另一个直接的替代方法是在应用基数规范后使用您的原始规范,例如
[
{
"operation": "cardinality",
"spec": {
"*": "MANY"
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"@": "[#2].&2"
}
}
}
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.