Home > Net >  Jolt Spec for nested arrays
Jolt Spec for nested arrays

Time:12-02

I have a Array of Arrays which have to be transformed using Jolt as shown below

Input:

{
  "ticket_fields": [
    {
      "url": "/api/v2/ticket_fields/1.json",
      "id": 1,
      "type": "subject",
      "title": "Subject",
      "raw_title": "Subject",
      "description": "",
      "active": true,
      "required": false,
      "regexp_for_validation": null,
      "removable": false,
      "agent_description": null,
      "creator_user_id": -1,
      "creator_app_name": null
    },
    {
      "url": "/api/v2/ticket_fields/2.json",
      "id": 2,
      "type": "status",
      "title": "Status",
      "raw_title": "Status",
      "description": "Request status",
      "raw_description": "Request status",
      "position": 3,
      "active": true,
      "required": false,
      "regexp_for_validation": null,
      "removable": false,
      "system_field_options": [
        {
          "name": "Open",
          "value": "open"
        },
        {
          "name": "Pending",
          "value": "pending"
        },
        {
          "name": "On-hold",
          "value": "hold"
        },
        {
          "name": "Solved",
          "value": "solved"
        }
      ],
      "creator_user_id": -1,
      "creator_app_name": null
    },
    {
      "url": "/api/v2/ticket_fields/3.json",
      "id": 3,
      "type": "tagger",
      "title": "Test",
      "raw_title": "Test",
      "description": "",
      "raw_description": "",
      "active": true,
      "required": false,
      "regexp_for_validation": null,
      "removable": true,
      "agent_description": null,
      "custom_field_options": [
        {
          "id": 1,
          "name": "Option1",
          "value": "Option1",
          "default": false
        },
        {
          "id": 2,
          "name": "Option2",
          "value": "Option2",
          "default": false
        },
        {
          "id": 3,
          "name": "Option3",
          "value": "Option3",
          "default": false
        }
      ],
      "creator_user_id": 332,
      "creator_app_name": null
    }
  ]
}

Jolt Spec:

[
  {
    "operation": "shift",
    "spec": {
      "ticket_fields": {
        "*": {
          "id": "field_list[&1].crm_field_identifier",
          "title": "field_list[&1].name",
          "required": "field_list[&1].required",
          "description": "field_list[&1].description",
          "creator_user_id": {
            "-1": {
              "@(2,type)": [
            "field_list[&3].target_property",
            "field_list[&3].ui_property"
          ],
              "#false": "field_list[&3].is_custom_field",
              "system_field_options": {
                "*": {
                  "$": "field_list[&5].options[&1].name",
                  "@": "field_list[&5].options[&1].value"
                }
              }
            },
            "*": {
              "@(2,id)": "field_list[&3].target_property",
              "@(2,type)": "field_list[&3].ui_property",
              "#true": "field_list[&3].is_custom_field",
              "custom_field_options": {
                "*": {
                  "$": "field_list[&5].options[&1].name",
                  "@": "field_list[&5].options[&1].value"
                }
              }
            }
          },
          "regexp_for_validation": "field_list[&1].validation_rules.ui_regex"
        }
      }
    }
  }
]

Current O/p:

{
  "field_list": [
    {
      "crm_field_identifier": 1,
      "name": "Subject",
      "required": false,
      "description": "",
      "target_property": "subject",
      "ui_property": "subject",
      "is_custom_field": "false",
      "validation_rules": {
        "ui_regex": null
      }
    },
    {
      "crm_field_identifier": 2,
      "name": "Status",
      "required": false,
      "description": "Request status",
      "target_property": "status",
      "ui_property": "status",
      "is_custom_field": "false",
      "validation_rules": {
        "ui_regex": null
      }
    },
    {
      "crm_field_identifier": 3,
      "name": "Test",
      "required": false,
      "description": "",
      "target_property": 3,
      "ui_property": "tagger",
      "is_custom_field": "true",
      "validation_rules": {
        "ui_regex": null
      }
    }
  ]
}

Expected O/P

{
  "field_list": [
    {
      "crm_field_identifier": 1,
      "name": "Subject",
      "required": false,
      "description": "",
      "target_property": "subject",
      "ui_property": "subject",
      "is_custom_field": "false",
      "validation_rules": {
        "ui_regex": null
      },
      "options": null
    },
    {
      "crm_field_identifier": 2,
      "name": "Status",
      "required": false,
      "description": "Request status",
      "target_property": "status",
      "ui_property": "status",
      "is_custom_field": "false",
      "validation_rules": {
        "ui_regex": null
      },
      "options": [
        {
          "name": "Open",
          "value": "open"
        },
        {
          "name": "Pending",
          "value": "pending"
        },
        {
          "name": "On-hold",
          "value": "hold"
        },
        {
          "name": "Solved",
          "value": "solved"
        }
      ]
    },
    {
      "crm_field_identifier": 3,
      "name": "Test",
      "required": false,
      "description": "",
      "target_property": 3,
      "ui_property": "tagger",
      "is_custom_field": "true",
      "validation_rules": {
        "ui_regex": null
      },
      "options": [
        {
          "name": "Option1",
          "value": "Option1"
        },
        {
          "name": "Option2",
          "value": "Option2"
        },
        {
          "name": "Option3",
          "value": "Option3"
        }
      ]
    }
  ]
}

Though there are conditional rules to map either "system_field_options" or "custom_field_options" to field_list.options in O/P they are not being populated. Any help as to why this didnt work and working solution is appreciated

CodePudding user response:

You can use this shift transformation spec

[
  {
    "operation": "shift",
    "spec": {
      "ticket_fields": {
        "*": {
          "id": "field_list[&1].crm_field_identifier",
          "title": "field_list[&1].name",
          "required|description": "field_list[&1].&",
          "regexp_for_validation": "field_list[&1].validation_rules.ui_regex",
          "#false": "field_list[&3].is_custom_field",
          "*_field_*": {
            "*": {
              "name|value": "field_list[&3].&(2,2).[&1].&" // restrict only to two attributes
            }
          }
        }
      }
    }
  }
]

In your case, should need to replace the tags

"system_field_options" with "@(2,system_field_options)" and

"custom_field_options" with "@(2,custom_field_options)"

in order to able to reach their original level.

  • Related