Dealing With JSON Files

Development & UX Researches
1 min readFeb 4, 2021

--

The BEST way of reading nodes of a JSON file

Having trouble reading JSON file nodes or validating one of the values? You are right here. It becomes hectic sometimes even though it seems a very easy thing to solve and even there are tons of GSON, JACKSON, Jayway dependencies are sitting there.

I came across this works for me as the BEST and of course for now :) For example, once you are given like the below file and finding the batters.batter[0].id node from the below JSON file:

{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
}

The best and easiest way for me is; Taking the path of the file and, copy the JSON content into https://jsonpathfinder.com/ for a better and visual representation.

String data = new String(Files.readAllBytes(Paths.get("src/main/java/org/api/json/mixpayload.json")));
Object nodeVal = JsonPath.read(data, "batters.batter[0].id");
System.out.println(nodeVal);

I dealt with the below ways also: More to come…

--

--

Development & UX Researches
Development & UX Researches

Written by Development & UX Researches

A self-learner in the programming and UX industry. Keep yourself updated in this journey together ;)