Search code examples
jsonjolt

JOLT to filter info without content/null


I have a JSON like this

{
  "entityType": "new",
  "id": 12345,
  "properties": {
    "person_file": "HRC12345",
    "CV/Resume": {
      "content": 1234,
      "type": "CV",
      "label": null
    },
    "Photo": {
      "content": "abcde",
      "type": "Photo",
      "label": "passphoto.jpg"
    },
    "medicalinfo": {
      "content": "abcdefg",
      "type": "medicalinfo",
      "label": "2023-08-29_16h51_58.jpg"
    },
    "Certificates": {
      "content": "abcdefghi",
      "type": "Certificates",
      "label": "resolution.docx"
    },
    "Results": [
      {
        "content": [
          "abcdefghijk"
        ],
        "label": [
          "wheyler.jpg"
        ]
      }
    ],
    "passport": [
      {
        "content": [],
        "label": []
      }
    ],
    "OtherInfo": [],
    "TestInfo": [],
    "PsychologicalExam": [],
    "Results.type": "Results",
    "TestInfo.type": "TestInfo",
    "OtherInfo.type": "OtherInfo",
    "PsychologicalExam.type": "Psychological Exam",
    "passport.type": "passport",
    "credentials": {
      "password": "xxxx",
      "username": "xxxx"
    }
  }
}

Its quiet messy cause the info doesn't appear in the same way, and I need to get something like this

[
  {
    "Type": "Photo",
    "content": "abcde",
    "label": "passphoto.jpg"
  },
  {
    "Type": "medicalinfo",
    "content": "abcdefg",
    "label": "2023-08-29_16h51_58.jpg"
  },
  {
    "Type": "Certificates",
    "content": "abcdefghi",
    "label": "resolution.docx"
  },
  {
    "Type": "Results",
    "content": "abcdefghijk",
    "label": "wheyler.jpg"
  }
]

The issue is when I'm receiving empty array or no content (example:OtherInfo; passport; PsychologicalExam ) the JOLT i'm using can't handle it and gives me a wrong result. Summarizing, I only need to keep the info that has content and label on it.


Solution

  • You can use the following transformation

    [
      {
        "operation": "shift",
        "spec": {
          "properties": {
            "*": { // for objects
              "content|label": {
                "@": "&2.&1"
              },
              "*": { // for arrays ( such as "Results" ) 
                "*": {
                  "*": "&3.&1"
                }
              }
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "~content|~label": "removeObject", // set them as "removeObject" if they're null 
            "con_lab": "=concat(@(1,content),@(1,label))"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "$": "@(1,con_lab).Type",
            "*": "@(1,con_lab).&"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*emoveObj*": { // remove the objects which contains
            // null values of "content" or "label"
            "": ""
          },
          "*": {
            "*": "&1.&"
          }
        }
      },
      { // reform as array of objects
        "operation": "shift",
        "spec": {
          "*": {
            "*": "[#2].&"
          }
        }
      },
      { // get rid of the attribute "con_lab"
        "operation": "remove",
        "spec": {
          "*": {
            "con_lab": ""
          }
        }
      }
    ]