Thiyagaraj Narayanan
Thiyagaraj Narayanan

Reputation: 331

Jolt Spec - Need help in getting Flattened Array

I am having some trouble with flattening the JSON. I have looked at examples and didn't get any closer as to what is mentioned above. I need to transform a JSON structure by using a JOLT spec. I use https://jolt-demo.appspot.com to test the following below.

JSON Input for a sample :

{
  "metadata": {
    "timestamp": "XXXXX"
  },
  "data": {
    "product": {
      "id": "XXXXX"
    },
    "storageLocations": [
      {
        "id": "XXXXX",
        "stocks": [
          {
            "stockStatus": "XXXXX",
            "quantity": [
              {
                "uom": "XXXXX",
                "isoUom": "XXXXX",
                "amount": "XXXXX"
              }
            ]
          },
          {
            "stockStatus": "XXXXX",
            "quantity": [
              {
                "uom": "XXXXX",
                "isoUom": "XXXXX",
                "amount": "XXXXX"
              }
            ]
          },
          {
            "stockStatus": "XXXXX",
            "quantity": [
              {
                "uom": "XXXXX",
                "isoUom": "XXXXX",
                "amount": "XXXXX"
              }
            ]
          }
        ],
        "dateModified": "XXXXX"
      },
      {
        "id": "XXXXX",
        "stocks": [
          {
            "stockStatus": "XXXXX",
            "quantity": [
              {
                "uom": "XXXXX",
                "isoUom": "XXXXX",
                "amount": "XXXXX"
              }
            ]
          },
          {
            "stockStatus": "XXXXX",
            "quantity": [
              {
                "uom": "XXXXX",
                "isoUom": "XXXXX",
                "amount": "XXXXX"
              }
            ]
          },
          {
            "stockStatus": "XXXXX",
            "quantity": [
              {
                "uom": "XXXXX",
                "isoUom": "XXXXX",
                "amount": "XXXXX"
              }
            ]
          }
        ],
        "dateModified": "XXXXX"
      },
      {
        "id": "XXXXX",
        "stocks": [
          {
            "stockStatus": "XXXXX",
            "quantity": [
              {
                "uom": "XXXXX",
                "isoUom": "XXXXX",
                "amount": "XXXXX"
              }
            ]
          },
          {
            "stockStatus": "XXXXX",
            "quantity": [
              {
                "uom": "XXXXX",
                "isoUom": "XXXXX",
                "amount": "XXXXX"
              }
            ]
          },
          {
            "stockStatus": "XXXXX",
            "quantity": [
              {
                "uom": "XXXXX",
                "isoUom": "XXXXX",
                "amount": "XXXXX"
              }
            ]
          }
        ],
        "dateModified": "XXXXX"
      }
    ],
    "dateModified": "XXXXX",
    "temporaryNegativeStock": "XXXXX"
  }
}

the tried Jolt Spec is :

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "storageLocations": {
          "*": {
            "@(3,metadata.timestamp)": "[&1].LastUpdated",
            "@(2,product.id)": "[&1].MaterialCode",
            "@(2,product.baseUoM)": "[&1].BaseUoM",
            "@(2,product.isoBaseUom)": "[&1].ISOBaseUoM",
            "@(2,location.id)": "[&1].LocationId",
            "@(id)": "[&1].StorageLocationId",
            "@(dateModified)": "[&1].StorageLocationLastUpdated",
            "stocks": {
              "*": {
                "quantity": {
                  "*": {
                    "@(amount)": "[&5].StockAmount",
                    "@(isoUom)": "[&5].StockISOUoM",
                    "@(uom)": "[&5].StockUoM",
                    "@(2,stockStatus)": "[&5].StockStatus"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

Ouput we are getting

[
  {
    "LastUpdated": "XXXXX",
    "MaterialCode": "na-XXXXX",
    "StorageLocationId": "XXXXX",
    "StorageLocationLastUpdated": "XXXXX",
    "StockAmount": [
      "0.000",
      "0.000",
      "0.000"
    ],
    "StockISOUoM": [
      "XXXXX",
      "XXXXX",
      "XXXXX"
    ],
    "StockUoM": [
      "XXXXX",
      "XXXXX",
      "XXXXX"
    ],
    "StockStatus": [
      "XXXXX",
      "XXXXX",
      "XXXXX"
    ]
  },
  {
    "LastUpdated": "XXXXX",
    "MaterialCode": "na-XXXXX",
    "StorageLocationId": "XXXXX",
    "StorageLocationLastUpdated": "XXXXX",
    "StockAmount": [
      "0.000",
      "0.000",
      "0.000"
    ],
    "StockISOUoM": [
      "XXXXX",
      "XXXXX",
      "XXXXX"
    ],
    "StockUoM": [
      "XXXXX",
      "XXXXX",
      "XXXXX"
    ],
    "StockStatus": [
      "XXXXX",
      "XXXXX",
      "XXXXX"
    ]
  },
  {
    "LastUpdated": "XXXXX",
    "MaterialCode": "na-XXXXX",
    "StorageLocationId": "XXXXX",
    "StorageLocationLastUpdated": "XXXXX",
    "StockAmount": [
      "0.000",
      "0.000",
      "0.000"
    ],
    "StockISOUoM": [
      "XXXXX",
      "XXXXX",
      "XXXXX"
    ],
    "StockUoM": [
      "XXXXX",
      "XXXXX",
      "XXXXX"
    ],
    "StockStatus": [
      "XXXXX",
      "XXXXX",
      "XXXXX"
    ]
  }
]

Need help in writing a jolt spec so that we get a flattened Array

Upvotes: 1

Views: 108

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65278

You can add one more shift transformation to separate the content of the currently generated three objects triple so as to generate 3 x 3 = 9 objects by walking along with a component of the arrays start with Stock(I've picked StockStatus in this case), then prune all of the key names through use of an extra shift transformation such as

[
  {
    "operation": "shift",
     <body of the current spec>
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "StockStatus": {
          "*": {
            "@(2,LastUpdated)": "&3.&1.LastUpdated",
            "@(2,MaterialCode)": "&3.&1.MaterialCode",
            "@(2,StorageLocationId)": "&3.&1.StorageLocationId",
            "@(2,StorageLocationLastUpdated)": "&3.&1.StorageLocationLastUpdated",
            "@(2,StockAmount[&])": "&3.&1.StockAmount",
            "@(2,StockISOUoM[&])": "&3.&1.StockISOUoM",
            "@(2,StockUoM[&])": "&3.&1.StockUoM",
            "@": "&3.&1.StockStatus"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

where &3 stands for going three levels up in order to reach the uppermost indices(they are 0,1,2), which are one level above the StockStatus key's level, and &1 to reach the indices of StockStatus array(they are 0,1,2 too) to separate each element groups into 3 x 3 = 9 different objects. The ampersands within the @(2,Stock....[&]) keys represent looking up the indices of StockStatus array as well.

Upvotes: 1

Related Questions