Skip to content
Advertisement

Parse JSON file with dynamic fields by java

How to parse JSON file with the object, which contains dynamic fields. As an example here: Here is “items”, which has objects with name(“1c68b853-2a07-4409-ad61-8650212f3260”) and body. And it’s more then 2 items.

{
    "id": "3c49a7aa-77e0-43cf-956c-a1ff970d7db2",
    "code": "citizenship_country",
    "name": "Гражданство",
    "items": {
        "1c68b853-2a07-4409-ad61-8650212f3260": {
            "name": {
                "id": "1c68b853-2a07-4409-ad61-8650212f3260",
                "value": "ДЖЕРСИ",
                "translation": "ДЖЕРСИ"
            },
            "modifiedDate": {
                "id": "1c68b853-2a07-4409-ad61-8650212f3260",
                "value": "мая 28, 2015",
                "translation": null
            },
            "id": {
                "id": "1c68b853-2a07-4409-ad61-8650212f3260",
                "value": "237",
                "translation": null
            }
        },
        "dfd37aec-4ae7-4d8b-8e92-3a25fab57dd7": {
            "name": {
                "id": "dfd37aec-4ae7-4d8b-8e92-3a25fab57dd7",
                "value": "РОССИЯ",
                "translation": "РОССИЯ"
            },
            "modifiedDate": {
                "id": "dfd37aec-4ae7-4d8b-8e92-3a25fab57dd7",
                "value": "мая 28, 2015",
                "translation": null
            },
            "id": {
                "id": "dfd37aec-4ae7-4d8b-8e92-3a25fab57dd7",
                "value": "185",
                "translation": null
            }
        }
    }
}

Advertisement

Answer

You can easily use the Map interface, to store the dynamic parts:

Root.java:

package com.example.trial.dto;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class Root {
    public String id;
    public String code;
    public String name;
    public Map<UUID, Map<String, Item>> items = new HashMap<>();
}

Item.java:

package com.example.trial.dto;

import java.util.UUID;

public class Item {
    public UUID id;
    public String value;
    public String translation;
}

MappingTest.java:

package com.example.trial.services;

import com.example.trial.dto.Root;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class MappingTest {

    ObjectMapper objectMapper = new ObjectMapper();

    @Test
    public void convert() throws IOException {
        Root root = objectMapper.readValue("{n" +
            "    "id": "3c49a7aa-77e0-43cf-956c-a1ff970d7db2",n" +
            "    "code": "citizenship_country",n" +
            "    "name": "Гражданство",n" +
            "    "items": {n" +
            "        "1c68b853-2a07-4409-ad61-8650212f3260": {n" +
            "            "name": {n" +
            "                "id": "1c68b853-2a07-4409-ad61-8650212f3260",n" +
            "                "value": "ДЖЕРСИ",n" +
            "                "translation": "ДЖЕРСИ"n" +
            "            },n" +
            "            "modifiedDate": {n" +
            "                "id": "1c68b853-2a07-4409-ad61-8650212f3260",n" +
            "                "value": "мая 28, 2015",n" +
            "                "translation": nulln" +
            "            },n" +
            "            "id": {n" +
            "                "id": "1c68b853-2a07-4409-ad61-8650212f3260",n" +
            "                "value": "237",n" +
            "                "translation": nulln" +
            "            }n" +
            "        },n" +
            "        "dfd37aec-4ae7-4d8b-8e92-3a25fab57dd7": {n" +
            "            "name": {n" +
            "                "id": "dfd37aec-4ae7-4d8b-8e92-3a25fab57dd7",n" +
            "                "value": "РОССИЯ",n" +
            "                "translation": "РОССИЯ"n" +
            "            },n" +
            "            "modifiedDate": {n" +
            "                "id": "dfd37aec-4ae7-4d8b-8e92-3a25fab57dd7",n" +
            "                "value": "мая 28, 2015",n" +
            "                "translation": nulln" +
            "            },n" +
            "            "id": {n" +
            "                "id": "dfd37aec-4ae7-4d8b-8e92-3a25fab57dd7",n" +
            "                "value": "185",n" +
            "                "translation": nulln" +
            "            }n" +
            "        }n" +
            "    }n" +
            "}", Root.class);
        objectMapper.writeValue(System.out, root);
    }
}

You can also, as well, create three different classes for name, modifiedDate, id, but this is enough to show the main idea of the answer.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement