5

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>

xploreraj
  • 3,792
  • 12
  • 33
  • 51
  • 3
    You can use a `Map` where the `empName` will be `key` and the `EmployeeDTO` object will be its value. – Mushif Ali Nawaz Sep 17 '19 at 11:18
  • 1
    thanks for chiming in. but i am looking for the json schema, not pojo. – xploreraj Sep 17 '19 at 15:54
  • I am not sure your question is clear. You want to build JSON dynamically and then generate schema for it? – Michał Ziober Sep 17 '19 at 16:44
  • Nope. I want to write the JSON schema which will be used by jackson to generate POJO sources which will be used in my code to populate data. So when I return the REST response, the entity will have this POJO, and it will be returned as JSON string in schema format. Bit clear now? – xploreraj Sep 17 '19 at 17:37
  • added a note at bottom of question. – xploreraj Sep 17 '19 at 17:46
  • I understand `empName` should be the key and the value should be rest JSON. But in your expected result I see `empName` as `john` but the key is `jdoe123` from where that comes in or it's typo ? – Achu Sep 17 '19 at 17:51
  • sorry, typo. edited. – xploreraj Sep 17 '19 at 17:59
  • Relevant? https://stackoverflow.com/questions/28697209/json-schema-for-dynamic-properties – Roddy of the Frozen Peas Sep 17 '19 at 18:02
  • 1
    Ooops... I expected json schema to play a part in this response process while it just generates the POJO. Suddenly the Map idea came to mind which @MushifAliNawaz already mentioned above. This solved my problem. So in my case, schema is used only for the DTO generation, which also wasnt mandatory if I had created the POJOs myself as I am only consumer of those. Thanks guys, and sorry for the trouble. I just got deviated from main issue to other things. – xploreraj Sep 17 '19 at 18:07
  • But anyways, if a json schema is possible, as i mistakenly asked, that will also be helpful for api response contracts. – xploreraj Sep 17 '19 at 18:13
  • @xploreraj I'm glad that my proposed solution solved your problem. – Mushif Ali Nawaz Sep 17 '19 at 18:50

1 Answers1

0

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()));
Mushif Ali Nawaz
  • 3,707
  • 3
  • 18
  • 31