skip to Main Content

Need help on writing JOLT spec :

Input sample:

{
  "key": [
    {
      "sku": "015110530",
      "location": "0635"
    }
  ],
  "value": [
    {
      "timestamp": 20230515094712,
      "supplies": [
        {
          "stock_type": "C",
          "requirement_category": "A06100021"
        },
        {
          "stock_type": "B",
          "requirement_category": "A06100022"
        }
      ]
    }
  ]
}

Output required:

[
  {
    "sku": "015110530",
    "location": "0635",
    "stock_type": "C",
    "requirement_category": "A06100021"
  },
  {
    "sku": "015110530",
    "location": "0635",
    "stock_type": "B",
    "requirement_category": "A06100022"
  }
]

could any one please help on the spec?

Tried below spec but only first array element is getting added with "key" attributes

[
  {
    "operation": "shift",
    "spec": {
      "key": {
        "*": {
          "sku": "[#2].sku",
          "location": "[#2].location"
        }
      },
      "value": {
        "*": {
          "supplies": {
            "*": {
              "stock_type": "[#2].stock_type",
              "requirement_category": "[#2].requirement_category"
            }
          }
        }
      }
    }
  }
]

Any suggestion or spec would be much appreciated.

Thanks
Mahendra

2

Answers


  1. Chosen as BEST ANSWER

    I am able to get the desired output when I made "key" as single record instead of array :

    [
      {
        "operation": "shift",
        "spec": {
          "value": {
            "*": {
              "supplies": {
                "*": {
                  "@(4,key.sku)": "[&1].sku",
                  "@(4,key.location)": "[&1].location",
                  "stock_type": "[&1].stock_type",
                  "requirement_category": "[&1].requirement_category"
                }
              }
            }
          }
        }
      }
      ]```
    

  2. One option is to make the shift transformation spec more dynamic by using

    [
      {
        "operation": "shift",
        "spec": {
          "value": {
            "*": {
              "supplies": {
                "*": {
                  "@4,key[0]": { "*": "[&1].&" }, // go four levels up the tree to grab the values of the attributes of the object nested within the "key" array
                  "*": "[&1].&" 
                }
              }
            }
          }
        }
      }
    ]
    

    the demo on the site http://jolt-demo.appspot.com/ is

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search