Home > OS >  JOLT transform table like JSON to well-formed JSON
JOLT transform table like JSON to well-formed JSON

Time:10-07

I'm new to jolt, how would I go about transforming this table like json, ignoring the Header RowType, into more well-formed json.

Input:

{
  "Rows": [
    {
      "RowType": "Header",
      "Cells": [
        {
          "Value": ""
        },
        {
          "Value": "1 Aug 23"
        }
      ]
    },
    {
      "RowType": "Section",
      "Title": "Income",
      "Rows": [
        {
          "RowType": "Row",
          "Cells": [
            {
              "Value": "Sales"
            },
            {
              "Value": "18494.43"
            }
          ]
        },
        {
          "RowType": "SummaryRow",
          "Cells": [
            {
              "Value": "Total Income"
            },
            {
              "Value": "18494.43"
            }
          ]
        }
      ]
    }
  ]
}

Expected output:

{
  "Income": {
    "Sales": "18494.43",
    "TotalIncome": "18494.43"
  }
}

In the original json, there are more Sections, but they follow the same sub-structure.

I've tried various options, but I don't seem to be able to pull the value from the Cells[0] as the key for the value of Cells[1].

CodePudding user response:

You can deep down upto innermost in order to reach the level of the Values those need to be extracted while seperating their respective objects by RowType(@(3,RowType).), and attributes by their indexes of the appearance order within the innermost Cells arrays(&&1->Value0 and 1) in the first shift transformation spec.

Then match the returned values within the second spec such that

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "*": {
                "*": {
                  "*": "@(3,RowType).&&1"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*ow": { //only RowTypes ending with "ow" provided that there might others at the same level
        "@Value1": "@Value0"
      }
    }
  }
]

the demo on the site enter image description here

  • Related