json 通过定位父数组对象向嵌套数组对象添加属性

rslzwgfq  于 2023-04-22  发布在  其他
关注(0)|答案(2)|浏览(133)

我想知道是否可以通过查看父数组对象来使用jolt在嵌套数组对象上添加属性
例如,这里是input.json,我想通过查看父所有者数组对象在pet对象中添加一个名为ownerName的元素。

Input.json

{
  "owners": [
    {
      "name": "John",
      "ownerId": 1,
      "children": [
        {
          "name": "Jack",
          "childId": 11,
          "pets": [
            {
              "type": "dog",
              "ownerId": 2
            },
            {
              "type": "cat",
              "ownerId": 2
            },
            {
              "type": "dog",
              "ownerId": 1
            }
          ]
        },
        {
          "name": "Jill",
          "childId": 12,
          "pets": [
            {
              "type": "dog",
              "ownerId": 1
            },
            {
              "type": "cat",
              "ownerId": 2
            },
            {
              "type": "dog",
              "ownerId": 1
            }
          ]
        }
      ]
    },
    {
      "name": "Jane",
      "ownerId": 2,
      "children": [
        {
          "name": "jack",
          "childId": 21,
          "pets": [
            {
              "type": "rabbit",
              "ownerId": 1
            },
            {
              "type": "dog",
              "ownerId": 1
            },
            {
              "type": "cat",
              "ownerId": 2
            }
          ]
        }
      ]
    }
  ]
}

预期输出

{
  "owners": [
    {
      "name": "John",
      "ownerId": 1,
      "childs": [
        {
          "name": "Jack",
          "childId": 11,
          "pets": [
            {
              "type": "dog",
              "ownerId": 2,
              "ownerName": "Jane"
            },
            {
              "type": "cat",
              "ownerId": 2,
              "ownerName": "Jane"
            },
            {
              "type": "dog",
              "ownerId": 1,
              "ownerName": "John"
            }
          ]
        },
        {
          "name": "Jill",
          "childId": 12,
          "pets": [
            {
              "type": "dog",
              "ownerId": 1,
              "ownerName": "John"
            },
            {
              "type": "cat",
              "ownerId": 2,
              "ownerName": "Jane"
            },
            {
              "type": "dog",
              "ownerId": 1,
              "ownerName": "John"
            }
          ]
        }
      ]
    },
    {
      "name": "Jane",
      "ownerId": 2,
      "childs": [
        {
          "name": "jack",
          "childId": 21,
          "pets": [
            {
              "type": "rabbit",
              "ownerId": 1,
              "ownerName": "John"
            },
            {
              "type": "dog",
              "ownerId": 1,
              "ownerName": "John"
            },
            {
              "type": "cat",
              "ownerId": 2,
              "ownerName": "Jane"
            }
          ]
        }
      ]
    }
  ]
}

我可以为此编写Jolt规范吗?

cetgtptt

cetgtptt1#

您可以使用此规范:
我们需要一个类似下面代码的查找来知道我们的1和2所有者名称是什么,我们想使用它们的名称。所以我们想在out spec的第一个shift操作中创建一个查找,我们可以在第二个shift操作中使用它:

{
  "ownerIds": {
    "1": "John",
    "2": "Jane"
  }
}

震动规格:

[
  {
    "operation": "shift",
    "spec": {
      "*": { // owners
        "*": { // 0, 1
          "*": "&2[&1].&",
          "@(0,name)": "ownerIds.@(0,ownerId)",
          "children": {
            "*": { // 0, 1
              "*": "&4[&3].childs[&1].&",
              "pets": {
                "*": { // 0, 1
                  "*": "&6[&5].childs[&3].&2[&1].&",
                  "ownerId": {
                    "@": "&7[&6].childs[&4].&3[&2].&1",
                    "@0": "&7[&6].childs[&4].&3[&2].lookup.@0"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "owners": {
        "*": { // 0, 1
          "*": "&2[&1].&",
          "childs": {
            "*": { // 0, 1
              "*": "&4[&3].&2[&1].&",
              "pets": {
                "*": { // 0, 1
                  "*": "&6[&5].&4[&3].&2[&1].&",
                  "lookup": {
                    "*": {
                      "@(8,ownerIds.&)": "&8[&7].&6[&5].&4[&3].ownerName"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]
9ceoxa92

9ceoxa922#

一个选项将通过将nameownerId匹配来开始,name将位于最外层以在下一转换中用作字典,通过再次将它们与ownerId属性匹配,同时通过使用@1表达式(例如

[
  {
    "operation": "shift",
    "spec": {
      "owners": {
        "@1": "", // replicate the top level, eg. the current JSON value
        "*": { // reform a dictionary
          "@name": "@ownerId"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "owners": {
        "*": { // represents the indexes of the top level array
          "*": "&2[&1].&", // replicate attributes/arrays/objects other than "children" 
          "children": {
            "*": { // represents the indexes of the top level array
              "pets": {
                "*": { // represents the indexes of the top level array
                  "type": "&6[&5].&4[&3].&2[&1].&",
                  "ownerId": {
                    "*": {
                      "@8,&": "&8[&7].&6[&5].&4[&3].ownerName"
                      // loop through the attributes of the dictionary 
                      // those are located 8 levels up
                    },
                    "@": "&7[&6].&5[&4].&3[&2].&" 
                      // replicate the "ownerId" attribute
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

相关问题