JOLT 转换以将列表分离为对象和条件值

JOLT transform to seperate list into objects and conditional values

我需要对某些 ​​JSON.

应用 JOLT 转换

示例 输入 JSON 是...

{
  "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"
}

预期输出将是:

{
  "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"
},

如何对这个 JSON 应用两个转换?

  1. allergen 列表中的每一项都成为它自己的 JSON 对象。
  2. doc_typetests 列表中的每一项都“转换为布尔值”。也就是说,如果 doc_type 列表包含 Allergist,那么生成的 JSON 对象将包含 diagnosed_by_allergist: true。如果短语“Allergist”不存在,则生成的对象不必包含 diagnosed_by_allergist: false。 (这更像是一个 条件 操作,而不是数据转换)。

在原文中JSON,所有后续数据均适用于每种过敏原。因此,例如,diagnosis_age 对生成的 PeanutsEggMilk JSON 对象应用相同的方法。

我可以使用什么 JOLT 转换规范将 allergen 列表“扁平化”为单独的对象和有条件地存在的字段?

您可以通过使用 "@" 通配符分别表示每个属性来遍历 allergen 列表,同时通过在第一个 shift transformation spec,第二个spec用于移除object key名称如

[
  {
    "operation": "shift",
    "spec": {
      "allergen": {
        "*": {
          "@": "&1.&2",
          "@(2,diagnosed_by_doc)": "&.diagnosed_by_doc",
          "@(2,diagnosis_age)": "&.diagnosis_age",
          "@(2,doc_type)": {
            "*": {
              "Allergist": {
                "#true": "&3.diagnosed_by_allergist"
              },
              "Dermatologist": {
                "#true": "&3.diagnosed_by_dermatologist"
              },
              "Other Healthcare Provider": {
                "#true": "&3.diagnosed_by_other_healthcare_provider"
              }
            }
          },
          "@(2,tests)": {
            "*": {
              "Skin prick test": {
                "#true": "&3.diagnosed_by_skin_prick_test"
              },
              "Blood tests": {
                "#true": "&3.diagnosed_by_blood_test"
              },
              "Oral food challenge": {
                "#true": "&3.diagnosed_by_oral_food_challenge"
              }
            }
          },
          "@(2,outgrown)": "&.outgrown"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]