0

I have a JSON Schema fetched from a DB, Which is now stored in a string in Java. I want to print only a section of schema but not all. How can I split the JSON/String and print.

I have tried converting the String back to JSON format. But not sure how to separate the required content. Also split method didn't worked for me as well.

Input:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "employee_id": {
      "type": "string"
    },
    "course_id": {
      "type": "string"
    },
    "college_id": {
      "type": "string"
    }
  },
  "required": [
    "employee_id",
    "course_id",
    "college_id"
  ]
}

Expected Result:

employee_id, course_id, college_id
IT_Guy
  • 79
  • 2
  • 11
  • You need a JSON parser. See [this question](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java). Does it answer your question? – Sweeper Jun 06 '19 at 09:48
  • 1
    Possible duplicate of [Simple JSON value parsing for Java 8](https://stackoverflow.com/questions/55581736/simple-json-value-parsing-for-java-8) – Austin Schaefer Jun 06 '19 at 09:48
  • Yes, I have used the gson and parsed the string back to JSON. – IT_Guy Jun 06 '19 at 09:51

4 Answers4

2

As your question doesn't provide any details on which library you are using to parse the JSON document, I have put together some approaches using popular JSON parsing libraries for Java.

JsonPath

It is pretty straightforward to be achieved with JsonPath:

List<String> required = JsonPath.parse(json).read("$.required");

Jackson

It also could be achieved with Jackson:

ObjectMapper mapper = new ObjectMapper();
List<String> required = mapper.convertValue(mapper.readTree(json).get("required"),
        new TypeReference<List<String>>() {});

Gson

In case you prefer Gson:

Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
List<String> required = gson.fromJson(jsonObject.getAsJsonArray("required"),
        new TypeToken<List<String>>() {}.getType());

JsonPath with Jackson or Gson

Depending on your needs, you could combine JsonPath with Jackson or Gson:

Configuration conf = Configuration.builder()
        .jsonProvider(new JacksonJsonProvider())
        .mappingProvider(new JacksonMappingProvider())
        .build();
Configuration conf = Configuration.builder()
        .jsonProvider(new GsonJsonProvider())
        .mappingProvider(new GsonMappingProvider())
        .build();
List<String> required = JsonPath
        .using(conf)
        .parse(json)
        .read("$.required", new TypeRef<List<String>>() {});
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • `List required = gson.fromJson(jsonObject.getAsJsonArray("required"), new TypeToken>() {}.getType());` this is ridiculous way to read it, you parse same json twice, what – Enerccio Jun 12 '19 at 09:57
0

I've made a helper library that uses gson and has ability to search json for subtrees/elements:

https://github.com/Enerccio/gson-utilities

In your case you would do

List<JsonElement> contents = JsonHelper.getAll(data, "key.required", x -> x instanceof JsonPrimitive);
System.out.print(contents.stream().map(JsonElement::getAsString).collect(Collectors.joining(", ")));

But you have invalid JSON, the valid version would be:

{
"key":{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "employee_id": {
      "type": "string"
    },
    "course_id": {
      "type": "string"
    },
    "college_id": {
      "type": "string"
    }
  },
  "required": [
    "employee_id",
    "course_id",
    "college_id"
  ]
}
}
Enerccio
  • 257
  • 1
  • 10
  • 23
  • Is there any other options, Which I can try ! – IT_Guy Jun 06 '19 at 10:33
  • I am not sure what you want to do, you can always just do it manually if your input is the same. Parse to JsonObject, then `JsonArray a = obj.get("key").get("required").getAsArray()` and then iterate over said array – Enerccio Jun 06 '19 at 10:36
0
String str=
 "{
"key":{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "employee_id": {
      "type": "string"
    },
    "course_id": {
      "type": "string"
    },
    "college_id": {
      "type": "string"
    }
  },
  "required": [
    "employee_id",
    "course_id",
    "college_id"
  ]
}
}
";  



        JSONObject jsonObj=new JSONObject(str);
       JSONObject keyJon= jsonObj.getJSONObject("key");
       String strUrl=keyJon.getString("$schema");
       System.err.println("str  "+strUrl);
Govind Sharma
  • 127
  • 1
  • 4
0

The below-mentioned method solved my problem.

        JSONObject jsonObject = new JSONObject(key);
        JSONArray req = jsonObject.getJSONArray("required");
        System.out.println("Required Parameters : "+req);
IT_Guy
  • 79
  • 2
  • 11