0

Is there any way to get the fields and the values into Java code. For example, I have the following JSON:

{
        "name":"Example",
        "description":"An example",
        "process_1": {
                "name":"My process-1",
                "image":"Docker Image",
                "command":["command", "file_name"],
                "arguments":["--arg"]
                },
        "process_2": {
                "name":"My process-2",
                "image":"Docker Image",
                "command":["command", "file_name"],
                "arguments":["--arg"]
                }
        .
        .

        .
        .
        "process_n": {
                "name":"My process-n",
                "image":"Docker Image",
                "command":["command", "file_name"],
                "arguments":["--arg"]
                }
}

Now I want to get all the keys as variables with their corresponding values in java. Note that the field names in JSON are not constant i.e., the JSON file is dynamically created with different field names every time. The name and description fields are constant as well as the fields inside the processes. But not the process fields.

For example, in Java it should look like:

String name = "Example"
String description = "An Example"
String process_1_name = "My process-1"
String process_2_command = ["command", "file_name"]

Also I don't know how can I store the cascading fields as in "process-1".

krrish_007
  • 21
  • 1
  • 6

1 Answers1

1

Your json is invalid - remove ',' after "arguments":["--arg"],.

For parsing you could use com.fasterxml.jackson.databind.ObjectMapper and Map:

public static void main(String[] args) throws IOException {
  String json =  "{\n" +
          "  \"name\":\"Example\",\n" +
          "  \"description\":\"An example\",\n" +
          "  \"process-1\": {\n" +
          "    \"name\":\"My process-1\",\n" +
          "    \"image\":\"Docker Image\",\n" +
          "    \"command\":[\"command\", \"file_name\"],\n" +
          "    \"arguments\":[\"--arg\"]\n" +
          "  },\n" +
          "  \"process-2\": {\n" +
          "    \"name\":\"My process-1\",\n" +
          "    \"image\":\"Docker Image\",\n" +
          "    \"command\":[\"command\", \"file_name\"],\n" +
          "    \"arguments\":[\"--arg\"]\n" +
          "  }\n" +
          "}\n";

    ObjectMapper mapper = new ObjectMapper();
    LinkedHashMap valueMap = mapper.readValue(json, LinkedHashMap.class);
    Object name = valueMap.get("name");
    Object description = valueMap.get("description");
    System.out.println("name: " + name);
    System.out.println("description: " + description);

    Map process1 = (Map) valueMap.get("process-1");
    Object process1Name = process1.get("name");
    Object process1Image = process1.get("image");
    System.out.println("process1Name: " + process1Name);
    System.out.println("process1Image: " + process1Image);

    mapper.writeValue(new File("./process-1.json"), process1);
    mapper.writeValue(new File("./process-2.json"), (Map) valueMap.get("process-2"));
}

Output:

name: Example
description: An example
process1Name: My process-1
process1Image: Docker Image
i.bondarenko
  • 3,442
  • 3
  • 11
  • 21
  • I have just updated the answer. ( "process-1" added) Have a look please. – i.bondarenko Sep 18 '19 at 06:00
  • I mentioned in the question that the field names are not constant, i.e., the JSON file is dynamically created with different field names every time. But in this is solution we need to know the field names beforehand. – krrish_007 Sep 18 '19 at 06:05
  • The name, description are constant but, the process fields names are not. – krrish_007 Sep 18 '19 at 06:07
  • You could move "image" and the other fields to config or parameters. But if you need just store "process-1" and "process-2" for example into a file you could use the same object mapper. Have a look at the updated answer. – i.bondarenko Sep 18 '19 at 06:18