Search code examples
jsonjoltdenormalizationjsonconverter

Need Jolt Spec to convert matrix json to denormalized json formart


Can anyone please help me a JOLT spec to convert my matrix type json to denormalized json. Please find the below my input json and my expected josn output.

Input Json:

[
  {
    "attributes": [
      {
        "name": "brand",
        "value": "Patriot Lighting"
      },
      {
        "name": "color",
        "value": "Chrome"
      },
      {
        "name": "price",
        "value": "49.97 USD"
      }
    ]
  },
  {
    "attributes": [
      {
        "name": "brand",
        "value": "Masterforce"
      },
      {
        "name": "color",
        "value": "Green"
      },
      {
        "name": "price",
        "value": "99.0 USD"
      }
    ]
  }
]

Expected Json output:

  [
    {
      "brand": "Patriot Lighting",
      "color": "Chrome",
      "price": "49.97 USD"
    },
    {
      "brand": "Masterforce",
      "color": "Green",
      "price": "99.0 USD"
    }
  ]

I was trying to build JOLT spec to convert this json. But challenge is the json which I have multiple tables with "attributes" tag.

Thanks in advance!


Solution

  • JOLT is not easy to use but I get a lot out of some other StackOverflow questions floating around and I just started reading up on the source code comments

    [
      {
        "operation": "shift",
        "spec": {
          // for each element in the array
          "*": {
            "attributes": {
              // for each element in the attribute
              "*": {
                // grab the value
                // - put it in an array
                //   - but it must be indexed by the "positions" found four steps back
                // - put the value in a key
                //   - that is determined by moving one step back and looking at member name
                "value": "[#4].@(1,name)"
              }
            }
          }
        }
      }
    ]
    

    This is seems very obscure at first glance but I hope the comments explain everything.

    Please go read on JOLT transformation to copy single value along an array

    Also this is almost mandatory for JOLT beginners https://docs.google.com/presentation/d/1sAiuiFC4Lzz4-064sg1p8EQt2ev0o442MfEbvrpD1ls/edit#slide=id.g9a487080_011

    If you need another example, I just answered a question here Nifi JOLT: flat JSON object to a list of JSON object

    And, probably your best friend can be found at https://jolt-demo.appspot.com