How to Parse Nested JSON using JAVA

Sharing buttons:

hello YouTube welcome back to my video

so in this video we are going to check

how to pass a JSON response using Java

so we'll be checking to pass how to pass

a simple JSON response and how to pass a

nested JSON response using Java so trust


this is the simple way you can pass a

JSON response using Java

okay so we'll be testing this passing

the json just one with the help of two

web services which is available online

free of course so will be our testing

with the help of IP a website known as

hype in 4d b.com in 4d p.com has a web

service which will return as a JSON

response for the HTTP request with we

have pass with the IP address current IP

address so it will return a JSON

response with the country country code

and the timezone and all the information

regarding the IP address so we'll be

testing that and the next one we'll be

testing is the google map api so it is

also a web service for which we'll pass

an HTTP request with Karen a latitude

and longitude the coordinate and we'll

return as response with the details of a

mint address and all the possible

address and other information from the

Google map API okay so we'll be testing

this both first let me show you the IP

in 40 b.com so as I told will test the

HTTP request through the browser and

read the JSON response from the browser

itself and then we'll move over to the

java program in my eclipse ide okay so

let me just login in the IP in 40 b.com

so you had to sign sign up here so it's

a basic sign of you how to input your

information here so just 40 accounts and

here you can just log in with your

username and password so off to you're

signed up you'll get an API key here

okay so this is the API key

here by which you have to pass your HTTP

request so here you can see here IP

location a please just click on there

and we can see here this one this is

HTTP request link and the parameters is

also key and IP address is there and we

can further the parameters is specified

here we can pass like key IP address

format and the callback you feel it will

occur so we are not having the Colburn

now we will set the format adjacent here

first let me copy this link completely

and passing it here and we'll pass that

as a state appear occurs and here this

the responses North in the JSON format

so we can say that the JSON format has

copy the format as the parameter and

give ampersand format is equal to Jason

here it is okay say I am passing it as

the HTTP request ID parameter format has

Jason so here it is this is the JSON

response of this HTTP request and this

is shown in my browser so we'll move

forward to the Java program and we'll

pass this using Java code okay so this

is my Eclipse IDE I have a project a

Java project the name of the jawbridge

it is past JSON response well inside my

package calm dog till effects welcome

this a class cat location from IP so

first one we are going to test this one

so in this one I have a main method here

and in which have set inside this one

have set the IP address a static IP

address and this is my API key and I'm

just putting the HTTP request with the

key and IPS the parameters and the

parameters format as Jason so this is a

stability bilikiss we are passing to

this link and I am just reading the

response at string okay so this is the

string so from here it will print out at

the response which is taken from this

Jason goes

and it will print out the value a string

from here okay so let's try this running

so I ought to run this just right-click

on the project and I just select run as

java application and just wait for your

project to get started yes and the bug

has been executed and you see can see

here the JSON response of the IP address

has been printed here and sees the same

JSON response which is the goal which we

got here let's check it so country name

is United State and the légion name is

Oklahoma and same here this country name

is United State and the name is Oklahoma

so I will move further to pause this

JSON response so we got this as a string

now so we'll have to import edges and

jar into approacher because we had to

the conversion of the string to JSON is

a sting a JSON objectives are done by a

the age' jessanja so I'm just importing

that into my build path or the project

so for that just right click on the

project go to build path configure build

path and here you can see here a button

to add external judge just click on that

and just now we get to the location

where your Java Jason is this o in my PC

it's in this pop so just enter here and

just press enter here it is Java

jessanja just double click on that and

import your project ok now just click OK

and till come in your reference

libraries now here we have to convert

that this string to JSON a bit Winston's

and just create it as a JSON object

equals new JSON object and we have to

pass the string inside this okay and by

this code we have just converted that

JSON string into a JSON object now from

this JSON object we have to call each

parameter or each variable from this

response one babe now we have to get

each variable or each parameter

according to that time so by looking

this this respond we can say that each

one of these is a string type as you can

see if it is string it will be B inside

the double course here it is okay

so I can just call each variable like as

a get string okay now in my next example

it won't be a get stream because the

response may have object arrays and

integers and double values everything

will be there so this is the basic

example so on I just shown you this one

so let's call each of these variables

from the code and pin all the values so

it will be like I have just coded to pin

all the variables or all the values from

the jason jason response you can see

here i am just getting that as getstring

to this jason object from this json

object and I'm just printing each values

one by one okay so now let me print this

one so let us execute this one and see

the output again so if I'm just running

it as java application

garetty so each of that I mean this

response has been complete converted

into a JSON object and this was as a

string it is converted JSON object and

that JSON object is passed and I be

caught although although all the

variables are printed here okay now this

is a straightforward like there is no

nest nested branches or any erase in

there let's take I'll show you one

sample JSON response which is a big one

and I have nested branches inside that

let's say how we can convert or how we

can pass that nester JSON response now

as I told this is the second project

which I'm going to test or I which I am

going to pass using Java so this is the

JSON response I called for an HTTP

request which I have Sun to Google map

API so this is basically as I told this

will return the current coordinate all

the possible address of any coordinates

which we are sending

so this is the coordinate which we pass

through as HTTP request and this is the

rest force now we have to pass this one

I mean this response using our Java code

okay so this is a little bit confusing

I'll explain how everything is done so

you can see this a web service just go

to Google and search for Google map API

and once you search it you will get the

first link itself Google map API just

click on there and you should be logged

in if you are not logged in just login

so after you login just click get

started and you can see here down

various web service API which Google map

is providing us so I have just used this

one google map geocoding API so inside

that one actually the reason there isn't

web service which we can pass like this

is the one no this is not down here it

is so if you are passing that lat and

long like this one and with your API key

so you can get your API key here just

click on get key and create a project if

you don't have any project and just

select any vendor project and just click

enable a piece okay generate an API key

for your project so we can just copy

paste this one with your API key in your

browser and you will get this response

okay so this is that one the way Hart

generated or how the path of the HTTP

request and got metal spoon now let's go

and pass this one so as the same project

I have just a crater on that class here

so this one is this one Gettler

coordinate location inside this one I

have just set my key as a string Kiwi a

string variable and in the URL I'm just

passing the geo code here and here it is

geo code and the key is here and the

same way it's a printing our response as

a string value here okay so let's try

this one it won the project and select

run as java application and wait for the

project to be

executed here it is so as you can see

here the response is completely printed

as a as a string value here now let's

try as I shown here I am just first

converting these to a JSON object so let

me copy this one place it here and yes

we have just converted all these these

responses to a JSON string I mean JSON

object now we have to check what all

what all the variables what all the

return type of this JSON object so the

best way to check what what is the

return type or what is what is the

variable what are the variable type

available in our project we can check

online it serve with our responds so the

best website for it is a JSON

online jason online editor dot or just

open this website where you can just

simply copy your JSON response which

which you have a screen just press

ctrl-a copy it completely come back to

here paste in this side just paste

everything as it is and just click on

this button so it will be formatted on

the right side of the screen so

basically this is our object I mean this

is our response so the response you can

see here the type is an object so

actually we have just this is the

response and first we are converting

their response to an object and then

just expand this one so in this object

there is two variables first one is are

the results so let's check what is the

type of the result so if we are just

clicking here and see here the type is

of array and here if you are clicking

this one this is type is auto which

means whatever it will depend upon the

variable key value of the status so it's

okay means it can be treated as a string


now the result it can be stated as a

JSON array only you can see it's an

array now just come back to our project

now from here just create a JSON array

object so it will be like jason array

Oh BJ Jason either we have just created

a new Jason adapter and from this one

there is an object just get that JSON

array so it will be like just Jason away

and the key value is this one the

results just copy that and paste it here

and let's try pending this this one let

me come in this one first

now if I'm just friending this JSON

array let's see what is coming so it

should print the JSON array only yes you

can see here the value printed is offer

that Jason array only so we have just

entered inside this one it is an array

only so you can see here there are 10

values let's check the values I mean how

many other elements are there in there

there so if I'm just clicking us giving

the println for the length off for the

Jason array so just copy this one on to

click length toward length here it is so

if I'm just printing this one it should

say if there is certain values here yes

the value is 10 now let's go inside and

take any of the elements so I'm going to

take the fifth element here so in this

fifth element this is what type just go

and say this is of object type so we can

just retrieve this element as an object

and let's see how it can be done here so

I'm just creating a new JSON object

because it is a new JSON object so let

me copy this till here and this is

actually an object to and to get the

fifth element you can use it like copied

object array and just kill and get JSON

object and here to pass the index so the

index here is five okay let's see so it

will take the fifth element of their

JSON response and let's try pending this

one so

or just give a value like element Fifth

Element fire now again I'm just reading

that it here object to now let's try

running this one you should bring me the

element first value yes it has printed

the element fifth value as you can see

the formatted address is Brooklyn NY one

one two five essay here this the

formatter is brutally in one if it is

for it's different okay so record the

element fifth now we can end up inside

this fifth element and check each every

each of these keys and values separately

now the address object is of type RL you

can see here

let's take here step type array okay I

just want to go inside this one and

print get this value long long name so

we first take the address object element

and inside that there is an one more

error right no inside there is object

now first we can take this address

components array so let me copy this one

come back to our project now I'm going

to create a new jason array object so

it's like jason arab J and the name I'm

giving it as address components array

and it can be taken from this one

element file just paste this one and get

JSON array and we can put the name here

as we can name the put here s at this

components okay so we got that address

components array so inside that dress

components I read that is this one of

type of object okay so we'll take the

object 0 to object here ok so we can

take yes we can take the zeroth object

here so

we'll be like mmm again a JSON object we

should create so it's a JSON object tree

and it's the Seurat objects or just get

JSON object as 0 and we call that a JSON

object till here zero JSON object so I'm

going to plane at the long name and

short name here so which can be directly

called as string okay as you can see 8

string so I'm just going to print that

two terms here stem dot out dot println

so just get the string and the value is

this one long name and the second value

is the short name let me bring that here

this is a long name this is a short name


so it will print at the long name and

short name we have reach inside this one

okay let me play in this let me try

running this one and let's see the

output again yes you can see here the

long name and short name has been

printed according to this one now let me

take the formatted address the formatted

address is of type ATO so we can take it

as string so it's all from the array 5

so it will be like this one from object

- I am just printing that value still

dot out dot println toward get a string

and we can paste the key as the

formatted address here and I'm just

putting it here ok formatted address now

what we have to play in this place

geometry is same like this one which we

taken from here we can first take the

geometry as type object and from inside

this one you can take the bounds as or

J okay so it's simple you can just go

inside one by one you can take the

parent object and go inside one by one

in the sequential order and get till the

last value so I'm not going to do that

also because it's a same as we had done

so I'm not going to do that so I have

just been to the format address now let

me take other place ID it's also a

string I guess to check it what is the

return type just caught no it's an auto

I think it will run with the string

value so here it is now let's try

running this if you are running as a

java application and wait here here it

is all the values has been printed as

per our name as per as we have code so

I'm not going to code every values in

this it's a little bit long one so hope

you got this how to pass a nested JSON

object I mean Esther JSON string how to

convert that into a JSON object using

Java and pass till the nested branch to

the last value or to the last branch how

to get that one by one so the best our

JSON online parser is this one takes an

editor dot editor online dot orgs you

can use this to pass any JSON response

hope you got this if you have any doubt

in this so please comment below I'll be

helping you I will share all the source

codes and all the sides I have used here

in my description you can have it and

please like and subscribe for my videos

and thanks