我在试着理解‘#’外卡的工作原理.

在准备ProductList时,我在规范中使用了‘#’,它起作用了,并且我得到了符合我预期的输出.但我不确定它是不是起作用了.

有没有人能帮我了解一下它的工作原理?

Here is the input JSON

{
  "orders": [
    {
      "order_parts": [
        {
          "id": "0001",
          "items": [
            {
              "id": "00101",
              "goodIdentificationList": [
                {
                  "goodIdentificationTypeId": "UPCA",
                  "idValue": "42684666380437"
                },
                {
                  "idValue": "V-ASHBY",
                  "goodIdentificationTypeId": "SHOPIFY_PROD_SKU"
                }
              ],
              "productName": "BLACK / 6 / 809"
            },
            {
              "id": "00102",
              "goodIdentificationList": [
                {
                  "goodIdentificationTypeId": "SHOPIFY_PROD_ID",
                  "idValue": "42684666380437"
                },
                {
                  "idValue": "V-ASHBY",
                  "goodIdentificationTypeId": "UPCA"
                }
              ],
              "productName": "BLACK / 6 / 809"
            }
          ]
        },
        {
          "id": "0002",
          "items": [
            {
              "id": "00103",
              "goodIdentificationList": [
                {
                  "goodIdentificationTypeId": "SHOPIFY_PROD_ID",
                  "idValue": "42684666380437"
                },
                {
                  "idValue": "V-ASHBY",
                  "goodIdentificationTypeId": "UPCA"
                }
              ],
              "productName": "BLACK / 6 / 809"
            }
          ]
        }
      ]
    }
  ]
}

以下规格预期:-

  1. 判断好的每个标识列表的映射,获取idValue where goodIdentificationTypeId - UPCA,并将ProductList作为gtin放入.

  2. 从Items列表中获取id,并将其作为Itemid放入ProductList中.

  3. 从Items列表中获取ProductName并将其作为名称放入ProductList中.

Jolt Spec is like below

[
  {
    "operation": "shift",
    "spec": {
      "orders": {
        "*": {
          "order_parts": {
            "*": {
              "items": {
                "*": {
                  "goodIdentificationList": {
                    "*": {
                      "goodIdentificationTypeId": {
                        "UPCA": {
                          "@(2,idValue)": "[&5].productList.[#8].gtin"
                        }
                      }
                    }
                  },
                  "id": "[&1].productList.[#4].itemId",
                  "productName": "[&1].productList.[#4].name"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "productList": {
          "*": "productList.[]"
        }
      }
    }
  }

]

By the above spec, I'm able to prepare the productList as I was expecting.
But want to understand the working of '#' here.

Output JSON

{
  "productList" : [ {
    "itemId" : "00101",
    "name" : "BLACK / 6 / 809"
  }, {
    "itemId" : "00103",
    "name" : "BLACK / 6 / 809"
  }, {
    "itemId" : "00102",
    "name" : "BLACK / 6 / 809"
  } ]
}

任何帮助都将不胜感激. 谢谢!

推荐答案

您主要需要此规格

[
  {
    "operation": "shift",
    "spec": {
      "order*": {
        "*": {
          "order*": {
            "*": {
              "items": {
                "*": {
                  "id": "&3.&1.&",
                  "prod*": "&3.&1.&"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "productList[]"
      }
    }
  }
]

其中,"items"个数组的索引(&1)和"order_parts"个数组的索引(&3)的组合处理三个单独对象的分离,然后在标记所需值(productList)的同时整理最里面的部分,并且组合那些新生成的对象将自发地形成array.

更换

"id": "&3.&1.&",
"prod*": "&3.&1.&"

使用

"id": "&3[&1].&",
"prod*": "&3[&1].&"

or

"id": "&3[#2].&",
"prod*": "&3[#2].&"

would handle exactly the same for this current case. But if there was no array the identifiers [&1] or [#2] would produce square brackets. The difference for right-hand-size usage of them is [&1] will traverse {, while [#2] will traverse both { and the current : characters in order to reach the target 使用in the current tree, and [&1] will use indexes 0,1,2, which starts from zero, everytime and this will generate some null components for generated arrays for some cases, while [#2] won't. Eg. On the RHS of the spec, # is only valid in the the context of an array, like "[#2]".What "[#2]" means is, go up the three levels(including the colon at the current level) and ask that node how many matches it has had, and then use that as an index in the arrays.

Json相关问答推荐

Jolt需要将缺少的值设置为空并保持相同的位置

我发现GoFr响应总是包含在数据字段中,如何返回自定义响应?

如何在Haskell中解析JSON,其中字段的名称可以是多个值之一,但应该转换为单个Haskell类型?

如何判断响应数组是否存在以及S是否有其他内容...?

Moshi:序列化 List 时出现问题

Jolt 变换以展平 json 字符串数组

如何在 jq 中按 IP 地址排序?

使用jq根据对象中键的值查找对象

如何在 JSonPath 中按值查找列表中的所有元素

jq - 将父键值提取为子元素旁边的逗号分隔值

jq可以在两个JSON对象列表中依次添加对象吗?

如果值不存在,则将值插入 JSON 数组

将文本转换为 python 列表

使用 System.Text.Json 序列化记录成员

PowerShell - JSON/PsCustomObject - 为什么我的数组被扁平化为一个对象?

Golang / Go - 如果 struct 没有字段,如何将其编组为空?

JSON 使用 simplejson 序列化 Django 模型

如何使用 Jackson 注释从 HttpResponse 反序列化 JSON 对象?

如何向 json IAM 策略添加 comments ?

如何使用 SwiftyJSON 遍历 JSON?