Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Parse implicit JSON list given by keys "1", "2"

I’m receiving this JSON from an API.

{
    "message" : "message",
    "1": {
        "packageCode": "packageCode1",
        "packageNum": "packageNum1"
    },
    "2": {
        "packageCode": "packageCode2",
        "packageNum": "packageNum2"
    }
}

Is it possible to convert it to a java object with the below attributes?

  • String message
  • Package [] packages

I’m using jackson-databind ObjectMapper.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you!

>Solution :

It is better to change the API, which returns this JSON to return an array of packages.

{
    "message" : "message",
    "packages": [
    {
        "packageCode": "packageCode1",
        "packageNum": "packageNum1"
    },
    {
        "packageCode": "packageCode2",
        "packageNum": "packageNum2"
    }]
}

If this cannot be changed, you’ll need to write a custom deserealizer by extending the StdDeserializer<T> class. You’ll have to programmatically inspect the JsonNode parse tree and assemble the object that you want.

This article explains how to do it and comes with a working code sample.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading