Home > Blockchain >  Transform Object in Array with Jolt
Transform Object in Array with Jolt

Time:08-09

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
    }
  ]
 }
}

CodePudding user response:

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 enter image description here

  • Related