skip to Main Content

I’m attempting to transform JSON to JSON by bringing the list values from the input JSON to the main object, but I’m finding the syntax confusing.

Input JSON:

[
  {
    "Request": {
      "Id": "id1",
      "name": "5-221734537593",
      "CreatedBy": "abc",
      "Updated": "02/28/2023 19:48:41",
      "UpdatedBy": "xyz",
      "ListOfNotes": {
        "Notes": [
          {
            "sent": "Y",
            "Id": "id2",
            "Created": "02/28/2023 19:46:00",
            "CreatedBy": "lmn",
            "Updated": "02/28/2023 19:46:06",
            "UpdatedBy": "efg"
          },
          {
            "sent": "Y",
            "Id": "id2",
            "Created": "02/28/2023 19:46:00",
            "CreatedBy": "lmn",
            "Updated": "02/28/2023 19:46:06",
            "UpdatedBy": "efg"
          }
        ]
      },
      "ListOfActions": {
        "Actions": [
          {
            "actId": "123",
            "actionPerformed": "y",
            "actionBy": "abc"
          },
          {
            "actId": "234",
            "actionPerformed": "y",
            "actionBy": "xyz"
          }
        ]
      }
    }
  }
]

Expected output:

[
  {
    "Id": "id1",
    "name": "5-221734537593",
    "CreatedBy": "abc",
    "Updated": "02/28/2023 19:48:41",
    "UpdatedBy": "xyz"
  },
  {
    "Notes": [
      {
        "sent": "Y",
        "Id": "id2",
        "Created": "02/28/2023 19:46:00",
        "CreatedBy": "lmn",
        "Updated": "02/28/2023 19:46:06",
        "UpdatedBy": "efg"
      },
      {
        "sent": "Y",
        "Id": "id2",
        "Created": "02/28/2023 19:46:00",
        "CreatedBy": "lmn",
        "Updated": "02/28/2023 19:46:06",
        "UpdatedBy": "efg"
      }
    ]
  },
  {
    "Actions": [
      {
        "actId": "123",
        "actionPerformed": "y",
        "actionBy": "abc"
      },
      {
        "actId": "234",
        "actionPerformed": "y",
        "actionBy": "xyz"
      }
    ]
  }
]

Spec tried:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "Request": {
          "Id": "[#].&",
          "name": "[#].&",
          "CreatedBy": "[#].&",
          "Updated": "[#].&",
          "UpdatedBy": "[#].&"
        }
      }
    }
  }
]

The output I am currently getting is missing the Notes and Actions fields, and I am unsure how to correctly transform them.

[
  {
    "Id": "id1",
    "name": "5-221734537593",
    "CreatedBy": "abc",
    "Updated": "02/28/2023 19:48:41",
    "UpdatedBy": "xyz"
  }
]

2

Answers


  1. You can use the following shift transformation spec

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": { // reduce two levels starting from the outermost layer
              "*": "&",   // else case
              "List*": "" // the objects/arrays start with "List"
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
  2. If the order of input JSON is different, the following JOLT spec can be more accurate.

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": "[0].&",
              "List*": "[]"
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search