Dot Net Dev 19
Dot Net Dev 19

Reputation: 387

How to flatten nested JSON with Jolt?

Hi I have following json and would like to unnest filed.So, I have an old order and new order for products. There could be more field to the json but level of nesting remains the same. Is it possible to do it via Jolt ? I can have fields like new_something and old_something in output

{
  "data": {
    "new": {
      "orders": {
        "type": "atype",
        "currency": "usd",
        "name": null,
        "code": null,
        "shipped": null,
        "identifier": "some uuid",
        "source": "webshop",
        "productinfo": [
          {
            "pType": "fan",
            "ts": "2024-11-15T13:33:16.047Z",
            "status": null,
            "seller": {
              "identifier": "uuid",
              "internacodes": [
                {
                  "type": "US",
                  "value": "1111"
                },
                {
                  "type": "UUID",
                  "value": "some uuid"
                }
              ]
            }
          },
          {
            "pType": "fan",
            "ts": "2024-11-25T13:33:16.047Z",
            "status": null,
            "seller": {
              "identifier": "uuid",
              "internacodes": [
                {
                  "type": "US",
                  "value": "1111"
                },
                {
                  "type": "UUID",
                  "value": "some uuid"
                }
              ]
            }
          }
        ],
        "unique_num": [
          {
            "type": "UUID",
            "value": "some uuid",
            "ts": "2024-11-13T25:15:47.919Z"
          },
          {
            "type": "product id",
            "value": "23456",
            "ts": "2024-11-22T15:15:46.912Z"
          }
        ]
      }
    },
    "old": {
      "orders": {
        "type": "atype",
        "currency": "usd",
        "name": null,
        "code": null,
        "shippedOffice": null,
        "identifier": "some uuid",
        "source": "webshop",
        "productinfo": [
          {
            "pType": "fan",
            "ts": "2024-11-15T13:33:16.047Z",
            "status": null,
            "seller": {
              "identifier": "uuid",
              "internacodes": [
                {
                  "type": "US",
                  "value": "1111"
                },
                {
                  "type": "UUID",
                  "value": "some uuid"
                }
              ]
            }
          }
        ],
        "unique_num": [
          {
            "type": "UUID",
            "value": "some uuid",
            "ts": "2024-11-13T25:15:47.919Z"
          },
          {
            "type": "product id",
            "value": "23456",
            "ts": "2024-11-22T15:15:46.912Z"
          }
        ]
      }
    }
  }
}

Please share advice and inputs . I tried via hardcoded operations in Jolt but field non nested field may be added .Nested one remains the same. I need output like this -

{

   
    "data.new.orders.type": "atype",
    "data.new.orders.currency": "usd"
    .....
    "data.new.orders.productinfo.seller.identifier": "seller_identifier"
}

Upvotes: 0

Views: 61

Answers (1)

Arnold Joseph
Arnold Joseph

Reputation: 545

Hi this spec can help you achieve it.

Giving you small piece with 2 fields:

Basically I am skipping "." operator with "\\." escape character "\\".

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "new": {
          "orders": {
            "type": "&3\\.&2\\.&1\\.&",
            "currency": "&3\\.&2\\.&1\\.&"
          }
        }
      }
    }
  }
]

For productinfo since it is array it will populate as with parent key as 0,1.

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "new": {
          "orders": {
            "type": "&3\\.&2\\.&1\\.&",
            "currency": "&3\\.&2\\.&1\\.&",
            "productinfo": {
              "*": {
                "pType": "&3\\.&2\\.&1\\.&"
              }
            }
          }
        }
      }
    }
  }
]

Example output:

{
  "data.new.orders.type" : "atype",
  "data.new.orders.currency" : "usd",
  "orders.productinfo.0.pType" : "fan",
  "orders.productinfo.1.pType" : "fan"
}

(with help from: https://jolt-demo.appspot.com/#inception )

Upvotes: 2

Related Questions