2

I was wondering if I can use jolt to add property on a a nested array object by looking at the parent array object

For example, here is the input.json and I want to add a element called as ownerName in the pet object by looking at the parent owners array object.

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
            }
          ]
        }
      ]
    }
  ]
}

expected output

{
  "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"
            }
          ]
        }
      ]
    }
  ]
}

Can I write Jolt spec for this?

Mohammadreza Khedri
  • 2,523
  • 1
  • 11
  • 22
Rock
  • 21
  • 3

2 Answers2

0

You can use this spec:

We need a lookup like the following code to know what is our 1 and 2 owner names, which we want to use the name of them. So we want to create a lookup in the first shift operation of out spec and we can use it in the second shift operation:

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

Jolt Spec:

[
  {
    "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"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]
Mohammadreza Khedri
  • 2,523
  • 1
  • 11
  • 22
0

An option would be starting by matching name with ownerId to be located at the outermost level to be used as a dictionary at the next transformation by matching them with the ownerId attributes again while keeping exact content as a whole through use of @1 expression such as

[
  {
    "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
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]
Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55