使用 JOLT 从 JSON 中删除 Null 属性和空数组

Removing Null Properties and empty Arrays from a JSON using JOLT

提问人:Mariano Baudena 提问时间:5/19/2023 最后编辑:Mariano Baudena 更新时间:5/20/2023 访问量:59

问:

我正在尝试使用 JOLT (v0.1.1) 来转换此 JSON 输入:

{
  "entityType" : "person",
  "id" : 78,
  "properties" : {
    "first_name" : "Ricardo",
    "last_name" : "Bochini",
    "company" : null,
    "title" : null,
    "phone_number" : null,
    "phone_type" : null,
    "city" : null,
    "state" : null,
    "country" : null,
    "email_home" : null,
    "email_work" : null,
    "websites" : null,
    "employments" : [ {
      "company_name" : "Disney",
      "start_date" : "08-Apr-2021",
      "end_date" : "",
      "title" : "dev"
    } ],
    "tags" : [ "Biotechnology", "Biochemistry", "Western Blotting" ],
    "willing_to_relocate_" : "true",
    "social_media_addresses" : "linkedin.com/sarasa",
    "areas_of_interest" : [ ],
    "industry" : [ ],
    "work_preference_candidate_16838217785051286" : [ ],
    "availability" : "",
    "job_id" : "4327447002",
    "initial_stage_id" : "6428145002",
    "resume" : {
      "content" : "JVBERi0xLjUKJcO",
      "type" : "resume",
      "label" : "FSD_CandidatesExport.pdf"
    },
    "education_table" : [ {
      "degree" : "",
      "degree_type" : "",
      "university" : "",
      "start_date" : "",
      "end_date" : "",
      "concentration" : ""
    } ],
    "form_search_type" : "Non-executive",
    "form_person_name" : "Luptiqay Dedzomu, Orunaze",
    "form_req_to_link" : "Test Job Integration",
    "adress_type" : "other",
    "website_address_type" : "other",
    "content_type" : "application/pdf",
    "form_background" : "dad",
    "form_technical_experience" : "technichal experience",
    "form_drivers" : "Drivers",
    "form_reasons" : "Reason",
    "form_ideal_role" : "Ideal role",
    "form_areas_of_interest" : [ "Research - Discovery", "Research - Preclinical" ]
  }
}

并得到这个输出:

{
  "entityType" : "person",
  "id" : 78,
  "properties" : {
    "first_name" : "Ricardo",
    "last_name" : "Bochini",
    "employments" : [ {
      "company_name" : "Disney",
      "start_date" : "08-Apr-2021",
      "title" : "dev"
    } ],
    "tags" : [ "Biotechnology", "Biochemistry", "Western Blotting" ],
    "willing_to_relocate_" : "true",
    "social_media_addresses" : "linkedin.com/sarasa",
    "availability" : "",
    "job_id" : "4327447002",
    "initial_stage_id" : "6428145002",
    "resume" : {
      "content" : "JVBERi0xLjUKJcO",
      "type" : "resume",
      "label" : "FSD_CandidatesExport.pdf"
    },
    "form_search_type" : "Non-executive",
    "form_person_name" : "Luptiqay Dedzomu, Orunaze",
    "form_req_to_link" : "Test Job Integration",
    "adress_type" : "other",
    "website_address_type" : "other",
    "content_type" : "application/pdf",
    "form_background" : "dad",
    "form_technical_experience" : "technichal experience",
    "form_drivers" : "Drivers",
    "form_reasons" : "Reason",
    "form_ideal_role" : "Ideal role",
    "form_areas_of_interest" : [ "Research - Discovery", "Research - Preclinical" ]
  }
}

我的意思是,从 JSON 中删除所有为 null 或空的属性。但是我找不到适用于具有空属性与数组混合的 JSON(有些是空的,有些是值的)的解决方案。

有谁知道我怎样才能得到一些东西来做到这一点?

我尝试了这个 JOLT:

[
  {
    "operation": "default",
    "spec": {
      "properties": {
        // for all keys that have a null value
        //  replace that null value with a placeholder
        "*": "DELETE"
      }
    }
  },

  {
    "operation": "shift",
    "spec": {
      "properties": {
        // match all keys
        "*": {
          // if the value of say "colorValue" is DELETE
          //  then, match but do nothing.
          "DELETE": null,
          // otherwise, any other values are ok
          "*": {
            // "recreate" the key and the non-PANTS value
            // Write the value from 2 levels up the tree the "@1"
            //  to the key from 3 levels up the tree => "&2".
            "@1": "&2"
          }
        }
      }
    }
  }
]

但是由于数组,它坏了。

数组 json null jolt

评论


答:

0赞 ElielBerra 5/20/2023 #1
[
  {
    // The logic of the arrays areas_of_interest, industry
    // and work_preference_candidate on the custom_fields
    // was already being handled on the previous JOLT of
    // the integration, review it
    "operation": "shift",
    "spec": {
      "properties": {
        "*": "properties.&",
        // If the below fields are null, remove them from the JSON
        "company|title|phone_*|city|state|country|email_*|websites": {
          "null": ""
        },
        // If the fields of the tables are empty strings,
        // send them to the field field_to_be_deleted,
        // but if they have a value leave them on their original location
        "employments|education_table": {
          "*": {
            "*": {
              "": {
                "@1": "field_to_be_deleted"
              },
              "*": {
                "@1": "properties.&4[&3].&2"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      // Remove the field field_to_be_deleted
      "field_to_be_deleted": ""
    }
  }
]

我真的配得上那笔奖金

评论

0赞 Mariano Baudena 5/20/2023
谢谢伊莱!不幸的是,它删除了除 social_media_addresses 之外的所有空属性或数组。但我们很接近!