jsonnestedtransformjolt

Jolt transform for nested json unexpected output


I have the below JSON and need to filter based on field

[
  {
    "firstname": "ABC",
    "lastname": "PQR",
    "id": 1,
    "address": {
      "city": "LA",
      "zipcode": "123",
      "addressType": "Home",
      "status": "Active"
    },
    "department": {
      "id": "12",
      "manager": "tyx",
      "extraInfo": "N/A"
    }
  },
  {
    "firstname": "MNO",
    "lastname": "LLL",
    "id": 2,
    "address": {
      "city": "TX",
      "zipcode": "234",
      "addressType": "Home",
      "status": "Active"
    },
    "department": {
      "id": "123",
      "manager": "tyxx",
      "extraInfo": "N/A"
    }
  }
]

I use the below jolt spec

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "address": {
          "status": {
            "Active": {
              "@2": "[]"
            }
          }
        }
      }
    }
  }
]

which gives Jolt spec test

I need the whole JSON not only field from address, I would like to have first name, last name,id and department in output ,please help.


Solution

  • You can start with nesting other elements within an others object, and go on like this :

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "[&1].others.&",
            "address": "[&1].&"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "address": {
              "status": {
                "Active": {
                  "@3,others": { "*": "&5.&" },
                  "@2": "&4.&3"
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": "[]"
        }
      }
    ]
    

    the demo on the site Jolt Transform Demo Using v0.1.1

    ( Notice below that the second status is Inactive within my demo example ) :

    enter image description here