Search code examples
arraysjsonobjectapache-nifijolt

Transform Object in Array with Jolt


I'm trying to convert a JSON object to an array contained one element

Input :

{
  "record": {
    "file_header": {
      "file_name": "TEST_FILE"
    },
    "scheda_contatto": {
      "phone_number": "5555-111-222-333",
      "type_call": 1
    }
  }
}

i want scheda_contatto to be an array with only 1 element

Output:

{
  "record": {
    "file_header": {
      "file_name": "TEST_FILE"
  },
  "scheda_contatto": [
    {
      "phone_number": "5555-111-222-333",
      "type_call": 1
    }
  ]
 }
}

Solution

  • You can use &1[0].& in order to nest all innermost sub-elements(&) of the single object by square-brackets([0]) with key name scheda_contatto(&1 under s*) while prefixing each value identifier by &1 and &2 respectively in order to replicate the wrapper key name record for both such as

    [
      {
        "operation": "shift",
        "spec": {
          "rec*": {
            "*": "&1.&",
            "s*": {
              "*": "&2.&1[0].&"
            }
          }
        }
      }
    ]
    

    the demo on the site http://jolt-demo.appspot.com/ is

    enter image description here