bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.54k stars 328 forks source link

Spec help: "flattening" a list to seperate objects, and conditional fields #1131

Open dylan-klomparens opened 2 years ago

dylan-klomparens commented 2 years ago

I need to apply a JOLT transform to some JSON.

Example input JSON is...

{
  "allergen" : [ "Peanuts", "Egg", "Milk" ],
  "diagnosed_by_doc" : "Yes",
  "diagnosis_age" : "3-5 years old",
  "doc_type" : [ "Allergist", "Dermatologist", "Other Healthcare Provider" ],
  "tests" : [ "Skin prick test", "Blood tests", "Oral food challenge" ],
  "outgrown" : "No"
}

The expected output would be:

{
  "allergen" : "Peanuts",
  "diagnosed_by_doc" : "Yes",
  "diagnosis_age" : "3-5 years old",
  "diagnosed_by_allergist": true,
  "diagnosed_by_dermatologist": true,
  "diagnosed_by_other_healthcare_provider": true,
  "skin_prick_test": true,
  "blood_test": true,
  "oral_food_challenge": true,
  "outgrown" : "No"
},
{
  "allergen" : "Egg",
  "diagnosed_by_doc" : "Yes",
  "diagnosis_age" : "3-5 years old",
  "diagnosed_by_allergist": true,
  "diagnosed_by_dermatologist": true,
  "diagnosed_by_other_healthcare_provider": true,
  "skin_prick_test": true,
  "blood_test": true,
  "oral_food_challenge": true,
  "outgrown" : "No"
},
{
  "allergen" : "Milk",
  "diagnosed_by_doc" : "Yes",
  "diagnosis_age" : "3-5 years old",
  "diagnosed_by_allergist": true,
  "diagnosed_by_dermatologist": true,
  "diagnosed_by_other_healthcare_provider": true,
  "skin_prick_test": true,
  "blood_test": true,
  "oral_food_challenge": true,
  "outgrown" : "No"
}

How can I apply two transforms to this JSON?

  1. Each item in the allergen list becomes its own JSON object.
  2. Each item in the doc_type and tests list get "converted to boolean". That is to say, if the doc_type list contains Allergist then the resulting JSON object would contain diagnosed_by_allergist: true. If the phrase "Allergist" is not present then it's not necessary for the resulting object to contain diagnosed_by_allergist: false. (This is more of a conditional operation than a data conversion).

In the original JSON, all the subsequent data applies to each allergen. So, for example, diagnosis_age applies the same to the resulting Peanuts, Egg, and Milk JSON objects.

What JOLT transform spec can I use to "flatten" the allergen list into individual objects and conditionally present fields?