Home > Back-end >  Parse JSON file with dynamic fields by java
Parse JSON file with dynamic fields by java

Time:05-23

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
            }
        }
    }
}

CodePudding user response:

You may parse manually th e Json. If you use jackson library (com.fasterxml.jackson.databind) for example you could

var objectMapper = new ObjectMapper();
JsonNode jsonRoot = objectMapper.readTree(jsonStr);

then you can dig into the structure

jsonRoot.path("items").path("1c68b853-2a07-4409-ad61-8650212f3260").asText()

you can also enumerate the members of an individuals node. Other libraries i.e. Gson have the same functionalities

CodePudding user response:

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\": null\n"  
            "            },\n"  
            "            \"id\": {\n"  
            "                \"id\": \"1c68b853-2a07-4409-ad61-8650212f3260\",\n"  
            "                \"value\": \"237\",\n"  
            "                \"translation\": null\n"  
            "            }\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\": null\n"  
            "            },\n"  
            "            \"id\": {\n"  
            "                \"id\": \"dfd37aec-4ae7-4d8b-8e92-3a25fab57dd7\",\n"  
            "                \"value\": \"185\",\n"  
            "                \"translation\": null\n"  
            "            }\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.

  • Related