英文:
Is this correct way to filter items from a list based on certain conditions in Jolt?
问题
最近,我们遇到了这样的情况,需要根据某些条件从输入的JSON中过滤一些元素。
我们想知道在Jolt中是否有可能实现这一操作。
经过一些探索,我们尝试更改现有的Jolt规范,但它没有产生预期的JSON输出。
为了实现所期望的JSON输出,我们使用了另一个转换操作来过滤输入的JSON,结合其他操作。
让我们通过一个示例来更清楚地说明。
假设我们的输入JSON如下所示。
[
{
"productId": "10002",
"shopifyProductId": "31675487486045",
"shopifyInventoryItemId": "33346316861533",
"isPreOrder": "N",
"isBackOrder": "Y",
"computedLastInventoryCount": 7956,
"productStoreId": "SG_STORE"
},
{
"productId": "10003",
"shopifyProductId": "31675487486046",
"shopifyInventoryItemId": "33346316861536",
"isPreOrder": "N",
"isBackOrder": "N",
"computedLastInventoryCount": 7958,
"productStoreId": "SG_STORE"
}
]
根据属性值 isPreOrder = N
和 isBackOrder = N
,我们需要过滤输入的JSON。
预期输出
[ {
"HC_PRODUCT_ID" : "10003",
"INVENTORY_ITEM_ID" : "33346316861536",
"PRODUCT_VARIANT_ID" : "31675487486046"
} ]
为了获得预期的输出,我们需要使用转换操作和一些通配符,如下所示。
Jolt规范
[
{
"operation": "shift",
"spec": {
"*": {
"isPreOrder": {// 检查属性isPreOrder
"N": {// 如果值为N
"@(2,isBackOrder)": {//向上检查两级,查看isBackOrder
"N": {// 如果值为N
"@4": "[]"// 向上四级,获取对象并将其放入列表
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"productId": "[&1].HC_PRODUCT_ID",
"shopifyInventoryItemId": "[&1].INVENTORY_ITEM_ID",
"shopifyProductId": "[&1].PRODUCT_VARIANT_ID"
}
}
}
]
第一个转换操作用于过滤列表,第二个转换操作用于修改属性。
请分享您的想法。
英文:
Recently, we came across a scenario in which we needed to filter some elements from the input JSON based on certain conditions.
We were wondering if this was possible in the Jolt.
After some exploration, we tried to change the existing Jolt spec, but it did not produce the expected output JSON.
To achieve the desired output JSON, we have used another shift operation to filter the input JSON, in combination with other operations.
Let’s make it more clear with an example.
Assume we’ve input JSON as shown below.
[
{
"productId": "10002",
"shopifyProductId": "31675487486045",
"shopifyInventoryItemId": "33346316861533",
"isPreOrder": "N",
"isBackOrder": "Y",
"computedLastInventoryCount": 7956,
"productStoreId": "SG_STORE"
},
{
"productId": "10003",
"shopifyProductId": "31675487486046",
"shopifyInventoryItemId": "33346316861536",
"isPreOrder": "N",
"isBackOrder": "N",
"computedLastInventoryCount": 7958,
"productStoreId": "SG_STORE"
}
]
On the basis of values of the attributes 'isPreOrder' = N
and 'isBackOrder' = N
we need to filter the input JSON
Expected Output
[ {
"HC_PRODUCT_ID" : "10003",
"INVENTORY_ITEM_ID" : "33346316861536",
"PRODUCT_VARIANT_ID" : "31675487486046"
} ]
To get the expected output we need to use shift operation with some wildcards, as shown below
Jolt Spec
[
{
"operation": "shift",
"spec": {
"*": {
"isPreOrder": {// check the attribute isPreOrder
"N": {// if the value is N
"@(2,isBackOrder)": {//go on the two level up check for the isBackOrder
"N": {// if the value is N
"@4": "[]"// go the four level up take the object and put it into the list
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"productId": "[&1].HC_PRODUCT_ID",
"shopifyInventoryItemId": "[&1].INVENTORY_ITEM_ID",
"shopifyProductId": "[&1].PRODUCT_VARIANT_ID"
}
}
}
]
First shift is to filter the list and second shift is to modify the attributes.
Please share your thoughts.
答案1
得分: 1
以下是您要翻译的内容:
[
{
"operation": "shift",
"spec": {
"*": {
"isPreOrder": {
"N": {
"@2,isBackOrder": {// 遍历树两层以达到输入中属性级别
"N": {
"@4,productId": "[#2].HC_PRODUCT_ID",
"@4,shopifyInventoryItemId": "[#2].INVENTORY_ITEM_ID",
"@4,shopifyProductId": "[#2].PRODUCT_VARIANT_ID"
}
}
}
}
}
}
}
]
另一个选项是首先使用这些属性的前缀,然后过滤出双重 N
,如下:
[
{
"operation": "shift",
"spec": {
"*": {
"*": "@1,isPreOrder.@1,isBackOrder.&"
}
}
},
{
"operation": "shift",
"spec": {
"@N.N": {
"productId": "[#].HC_PRODUCT_ID",
"shopifyInventoryItemId": "[#].INVENTORY_ITEM_ID",
"shopifyProductId": "[#].PRODUCT_VARIANT_ID"
}
}
}
]
英文:
You can use the following which combines those two conditionals within a single shift transformatio spec
[
{
"operation": "shift",
"spec": {
"*": {
"isPreOrder": {
"N": {
"@2,isBackOrder": {// traverse tree two levels to reach the level of the attribute within the input
"N": {
"@4,productId": "[#2].HC_PRODUCT_ID",
"@4,shopifyInventoryItemId": "[#2].INVENTORY_ITEM_ID",
"@4,shopifyProductId": "[#2].PRODUCT_VARIANT_ID"
}
}
}
}
}
}
}
]
Another option uses prefixing by those attributes first, then filter out
by double N
such as
[
{
"operation": "shift",
"spec": {
"*": {
"*": "@1,isPreOrder.@1,isBackOrder.&"
}
}
},
{
"operation": "shift",
"spec": {
"@N.N": {
"productId": "[#].HC_PRODUCT_ID",
"shopifyInventoryItemId": "[#].INVENTORY_ITEM_ID",
"shopifyProductId": "[#].PRODUCT_VARIANT_ID"
}
}
}
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论