Home > database >  Jolt transform expand dot as a JSON Object
Jolt transform expand dot as a JSON Object

Time:02-04

Is there a way in Jolt to convert a dot operator in JSON value to a nested JSON object while transforming the input ?

For example in the below json I would like target_property of id=2 to be converted to a json object as shown in the expected O/P. Any help is appreciated

Input:

{
  "name": "Test",
  "email": "Test",
  "form_id": "123",
  "field_list": [
    {
      "id": 1,
      "value": "Test Subject",
      "is_custom_field": false,
      "target_property": "subject"
    },
    {
      "id": 2,
      "value": "Test Description",
      "is_custom_field": false,
      "target_property": "comment.body"
    }
  ]
}

Jolt Transform tried

[
  {
    "operation": "shift",
    "spec": {
      "form_id": "ticket.ticket_form_id",
      "name": "ticket.requester.name",
      "email": "ticket.requester.email",
      "field_list": {
        "*": {
          "is_custom_field": {
            "true": {
              "@(2,target_property)": "ticket.custom_fields[&3].id",
              "@(2,value)": "ticket.custom_fields[&3].value"
            },
            "*": {
              "@(2,value)": "ticket.@(3,target_property)"
            }
          }
        }
      }
    }
  }
]

Current Output

{
  "ticket" : {
    "ticket_form_id" : "123",
    "requester" : {
      "name" : "Test",
      "email" : "Test"
    },
    "subject" : "Test Subject",
    "comment.body" : "Test Description"
  }
}

Expected Output

{
  "ticket": {
    "ticket_form_id": "123",
    "requester": {
      "name": "Test",
      "email": "Test"
    },
    "subject": "Test Subject",
    "comment": {
      "body": "Test Description"
    }
  }
}

CodePudding user response:

You can add another shift transformation spec such that

{
  "operation": "shift",
  "spec": {
    "*": {
      "*": "&1.&",
      "*.*": "&1.&(0,1).&(0,2)"
    }
  }
}

where

  • "*.*" represents the attributes having a dot within them. the zeroes within &(0,..) expressions stand for the current level
  • 1,2 as second arguments of them represent the first and the second pieces of the splitted keys (in this case comment.body) respectively.
  • Related