Dealing With JSON Files

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…

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Find recurrence in DP

NOTE: Where to from here?

Starting with Flutter: How to embed a webview

Arrays Talking to Arrays

Don't marry your little dependencies

Form Builder for TailwindCss available on ActionForms.io for FREE

Baby-step towards Augmented Reality/Virtual Reality with THREE.JS.

Upload Images to Cloudinary using NodeJS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sdet & Updates

Sdet & Updates

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

More from Medium

How to install Tailwind CSS in Angular 11.2

Using git in your group projects

Configuring & Optimizing tempdb

What is REST API?