How to generate JSON schema of object type with dynamic key names?

xploreraj :

Say I have following json:

{
    "empName": "rameshp",
    "designation": "SE1",
    "skills": [
        {
            "id": 2,
            "rating": 4,
            "skillName": "Node.js 7",
            "skillCategory": "Programming"
        }
    ]
}

Now there could be multiple objects like above, so I want to reference above schema which can be generated from https://www.jsonschema.net, but I want to have the empName as object keys and above as value, so it will be an object with multiple unique keys (empName) and the value will be referencing above schema. Consider below example, when the POJO is serialized to JSON by Jackson, it should be like below:

{
    "rameshp": {
        "empName": "rameshp",
        "designation": "SE1",
        "skills": [
            {
                "id": 2,
                "rating": 4,
                "skillName": "Node.js 7",
                "skillCategory": "Programming"
            }
        ]
    },
    "john": {
        "empName": "john",
        "designation": "SE2",
        "skills": [
            {
                "id": 2,
                "rating": 4,
                "skillName": "Node.js 7",
                "skillCategory": "Programming"
            }
        ]
    }
}

What should be the overall schema for this scenario (the JSON schema)? Ultimately, jackson will be used to generate the POJO for this schema and used in the REST APIs that I have created.

EDIT: (trying to clarify)

Imagine a List<Map<k, v>> as my data. I want to have json schema for this whose output json will have the keys as the object names, like above json example.

Solution: Mushif's comment to this question is the answer. For the value part, jsonschema can be used. Map<String, EmployeeDTO>

Mushif Ali Nawaz :

Initially, I added the comment to the question. Which turns out to be the solution to this question (as mentioned in question's description). I think I should further expand my proposed solution.

DTO classes:

public class EmployeeDTO {
    String empName;
    String designation;
    List<SkillDTO> skills;
    // getter/setters here
}

public class SkillDTO {
    Integer id;
    Integer rating;
    String skillName;
    String skillCategory;
    // getter/setters here
}

Transforming JSON:

Now after parsing the JSON to these DTOs you can simply collect them to Map based on empName field using Stream API:

List<EmployeeDTO> jsonSchema; // parsed JSON List

Map<String, EmployeeDTO> result = jsonSchema.stream().collect(Collectors.toMap(EmployeeDTO::getEmpName, Function.identity()));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=163803&siteId=1