Home > Software engineering >  JOLT split flat object into key/value array
JOLT split flat object into key/value array

Time:05-14

I'd like to split simple flat object into array, so each key/value appear as array element. Example:

Input

{ 
   "FIRST_NAME": "John",
   "LAST_NAME": "Doe"
}

Desired output:

[
  {
    "key": "FIRST_NAME",
    "value": "John"
  },
  {
    "key": "LAST_NAME",
    "value": "Doe"
  }
]

Tried various configs but couldn't get anywhere close.

CodePudding user response:

You can determine each key-value pair through use of $ and @ wildcards respectively within shift transformation spec such as

[
  { 
    "operation": "shift",
    "spec": {
      "*": {
        "$": "[#2].key",
        "@": "[#2].value"
      }
    }
  }
 ]

where

[#2] on the right hand side of the spec represents going up two levels up by traversing one colon(:) and an opening curly bracket({) and grabbing the indices of the arrays for added key and value nodes respectively as using them along with above mentioned wildcards.

the demo on the site enter image description here

  • Related