Simplifying Google Sheet JSON using Jolt

218 views Asked by At

I have gone through most of the test cases in Jolt that seem to serve as examples, but cannot find anything that does what I am looking for. For background, the source code is a stripped version of what comes from a google sheet. So rows and columns.

Source JSON:

[
  {
    "values": [
      {
        "formattedValue": "ACHME - Thailand"
      },
      {
        "formattedValue": "5,368.11"
      },
      {
        "formattedValue": "17.09%"
      }
    ]
  },
  {
    "values": [
      {
        "formattedValue": "ACHME-B2A"
      },
      {
        "formattedValue": "101.47"
      },
      {
        "formattedValue": "0.32%"
      }
    ]
  },
  {
    "values": [
      {
        "formattedValue": "ACHME-B2E"
      },
      {
        "formattedValue": "83.79"
      },
      {
        "formattedValue": "0.27%"
      }
    ]
  }
]

Desired Output:

[
  {
   "row": [
     "ACHME - Thailand",
     "5,368.11",
     "17.09%"
   ]
  },
  {
    "row": [
      "ACHME-B2A",
      "101.47",
      "0.32%"
    ]
  },
  {
    "row": [
      "ACHME-B2E",
      "83.79"
      "0.27%"
    ]
  }
]

I have tried this Jolt that gets me most of the way there, but I am still losing the row boundaries. It all comes out with one 'row' element and all of the values in an array.

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "values": {
          "*": {
            "formattedValue": "row[]"
          }
        }
      }
    }
  }
]
1

There are 1 answers

1
Milo S On BEST ANSWER

Spec

[
  {
    "operation": "shift",
    "spec": {
      "*": { // row index
        "values": {
          "*": { // column index
            "formattedValue": "[&3].row[&1]"
          }
        }
      }
    }
  }
]

Note, this assumes that all the incoming "values" arrays are the same length.