lundi 29 novembre 2021

Parse JSON document based on predefined JSON mapping document in Java

I want to get some idea if there is any library in Java that I can use to parse a JSON document based on a predefined JSON mapping document, or only way to achieve this is to write custom java code.

If it's required to write Java custom code what type of design pattern or data structure I should follow. Any advice will be very much appreciated.

Example -

Input JSON document -

{
  "basicInfo": {
    "name": "name",
    "age": "25",
    "address": "address"
  },
  "education": [
    {
      "ug": {
        "unversity": "university",
        "major": [
          "cs",
          "ds"
        ],
        "year": "2012"
      },
      "pg": {
        "unversity": "university",
        "major": [
          "cs",
          "ds"
        ],
        "year": "2015"
      }
    }
  ]
}

Predefined JSON mapping document

{
    "definitions": {
      "basicInfo": {
        "maxOccurance": "1",
        "fields": [
          {
            "key": "name",
            "type": "S",
            "lenght": "50",
            "usage": "M",
            "maxOccurrance": "1"
          },
          {
            "key": "age",
            "type": "S",
            "lenght": "3",
            "usage": "O",
            "maxOccurrance": "1"
          }
        ]
      },
      "education": {
        "maxOccurance": "10",
        "fields": [
          {
            "pg": {
              "maxOccurance": "1",
              "fields": [
                {
                  "key": "university",
                  "type": "S",
                  "lenght": "50",
                  "usage": "M",
                  "maxOccurrance": "1"
                },
                {
                  "key": "major",
                  "type": "S",
                  "lenght": "3",
                  "usage": "O",
                  "maxOccurrance": "1"
                }
              ]
            }
          }
        ]
      }
    }
  }

Expected output JSON document

{
    "basicInfo": {
      "name": "name",
      "age": "25"
    },
    "education": [
      {
        "pg": {
          "unversity": "university",
          "major": "cs"
        }
      }
    ]
  }

Aucun commentaire:

Enregistrer un commentaire