How to parse dynamic and nested JSON in java? - Rest assured API automation framework

How to parse dynamic and nested JSON in java? - Rest assured API automation framework


How to parse dynamic and nested JSON in java? - Rest assured API automation framework

Rest Assured is very popular in API Test Automation. REST Assured API can be used to invoke REST web services and match response content to test them. This video is about how we can parse JSON data to Java Object where keys are dynamically created.


✅ In this Rest-Assured video, we will be learning,
1. How to parse dynamic JSON?
2. How to parse nested JSON?
3. How to write a recursive Code?
4. Code debugging.


🔥🔥🔥 One to One connect with Fun Doo Testers : https://topmate.io/fundootesters

✅ If you like my videos, please consider buying me a coffee: https://www.buymeacoffee.com/fundoote

Good books to read :
✅ Head First Java: https://amzn.to/2ZQlXqI
✅ Cracking Coding interviews: https://amzn.to/3bDX8Un


✅ For business write us on: [email protected]
✅ Say hello to us, and stay updated!

Facebook: https://www.facebook.com/fundootesters
Instagram: https://www.instagram.com/fundootesters/
LinkedIn: https://www.linkedin.com/company/fund
Blog: https://medium.com/@fundootesters #Restassured #API #fundootesters
GitHub: https://github.com/Fundootesters/Rest



GEAR USE:
○ My Camera: https://amzn.to/3e2UqL7
○ Microphone: https://amzn.to/3bSUvOq
○ Microphone:https://amzn.to/2ZXuo3s
○ Tripod: https://amzn.to/2PqmTQN


DISCLAIMER
Links included in this description might be affiliate links. If you purchase a product or service with the links that I provide I may receive a small commission. There is no additional charge to you! Thank you for supporting me so I can continue to provide you with free content each week!


#Json #RestAssured #FunDooTesters


Content

0.76 -> Welcome to Fun Doo tester in this session , we are going to discuss very important topic
5.64 -> which is parsing
7.17 -> dynamic json file.
8.44 -> Most of the interview, we are seeing this kind of questions they are asking how to parse
13.52 -> dynamic json
14.52 -> or how to parse nested json?
16.059 -> So here i am having few different types of JSON.
19.42 -> So this is my first json which is having starting with the json one object followed by json
26.09 -> array
27.09 -> and inside we have gain json object.
30.91 -> Again json object.
32.09 -> So this is it is starting with the first json object followed by data.
37.71 -> I do different.
40.309 -> json array.
42.05 -> Again I have json object.
44.19 -> Here inside this array i have two json object.
47.739 -> Inside this json object
49.769 -> Again I am having json array and again json object.
55.299 -> So here I am having multiple kind of nested json objects.
59.559 -> So insert one this json object I'm having another object of any other object to it is
64.019 -> like that if
65.019 -> I'm going to give another example.
66.75 -> This is again little simple json object followed by json array area - json object - json object
74.86 -> and json array.
75.89 -> So this like this kind of hierarchy.
77.85 -> This is a little complex.
80.53 -> So if I'm going to do this json objects I can see this starting with the json object.
87.33 -> Followed by json array
88.9 -> So this is my json array inside these so search results.
92.25 -> So this my search results.
93.77 -> Inside search result, I am having three different objects
97.67 -> so this is my first product.
99.59 -> This is second product and this will be my third product.
105.17 -> So inside each product if I'm going to elaborate this I can see here.
110.9 -> This is my key and value pairs so key and value key and value.
114.26 -> Again the in question mark I have another json object to question mark having different
122.25 -> three
123.25 -> different json object.
125.3 -> So they started with the three values so one first is .
129.649 -> This one is followed by key inside key I'm having is a design object I am having value
140.26 -> as a json object.
141.349 -> So this is a little complex one.
144.48 -> If I'm going to another one
148.549 -> I'm using the same json but I have made it little complex so in last section and what
154.98 -> I did.
155.98 -> Yeah so in this JSON I have only two keys and value json object.
162.659 -> but i have, inside the key also.
166.59 -> This key, i have added
168.079 -> one more JSON object inside to make more complex.
172.459 -> Json Key.
174.76 -> Json object so we able to deal with all these types of JSON and we will learn how to parse
180.98 -> these types of JSON so first what I'm going to do?
184.239 -> I'm going to create one static method -get key
187.319 -> So our main object is to get the key our json file file so here my input will be JSON object
196.389 -> so whatever
197.389 -> json as a response I'm getting, I'm going to use it as an object as a key and I'm going
204.98 -> to use
205.98 -> json object so this is my utility org [dot] json [dot] json objects
211.689 -> and so I'm going to pass value as a key and I'm going to create a main method so main
217.9 -> method I have
218.9 -> to pass any json so first we are starting with the simple JOSN also will be able to
223.469 -> understand.
224.529 -> So this is my first JSON I'm taking and I'm initialize is a string value -string and I'm
232.2 -> calling this
233.2 -> method get key method and this method is accepting json objects so first first what i will convert
240.319 -> my string into json
243.359 -> Json object is equal to input json object
252.12 -> So this way I can convert my string to json . First thing we are going to check that in
257.82 -> our JSON whatever
258.91 -> value we are going to use
260.87 -> So my key will be, in outer side we have four keys, claim loos type cd, claim type, claim
270.27 -> reason
271.27 -> policy number.
272.27 -> So first we're going to check if json is having this particular key or not?
276.57 -> json [dot]
278.75 -> key.
279.84 -> So first we are going to json is having a key or not?
282.65 -> If json is having the key then we are going to store value as a boolean exist.
288.63 -> So if keys exist then we can store as a boolean value and if it is exist then based on this
296.21 -> we are going
297.21 -> to perform two operation.
298.24 -> If not exist first one is not exist if not exist then we are going to perform some operation,
308.76 -> else
310.29 -> we are going to just bring that value if it is available then we are going to print that
314.9 -> value, To print
315.9 -> value, I'm going to create one more method which is public static.
319.34 -> Again public static method.
322.32 -> Public static void parse object.
326.69 -> So I will be having objects all the time giving a name as it is an object and input will be
332.36 -> as input
333.36 -> will remain same.
334.36 -> So I'm going to use this input
339.69 -> and I'm going to pass this json on so simply I'm going to print that will you because this
344.83 -> json is having
345.83 -> that value.
347.47 -> so I'm gonna print this value and I call this method here, json and key
353.96 -> That's it.
354.96 -> So if I'm going to simplify I'm going to run this class and I'm going for this one claim
359.56 -> reason.
360.56 -> It will print the data and print that we can do one more thing to Jasn [dot] get the value.
368.85 -> So we will get value as well, if i am going to run this
373.72 -> Will get the value as well.
375.49 -> Value is zero one one.
377.101 -> But if I'm going to use country then it will fail.
382.83 -> so simple thing will not work here.
387.12 -> It will fail because it goes inside the exist part and we don't have any such code.
393.5 -> SO it will fail.
394.5 -> So our objective is to get the value.
396.64 -> So I'm removing this line now our logic starts from here.
402.34 -> Now what I'm going to do I'm going to initialize iterator here.
406.11 -> So this is again one more cocept why i am going to use question mark in diamond bracket?
410.95 -> This is the generic concept and I'm going to initialize one more variable with this
415.61 -> string nextkey.
416.74 -> So I'm storing multiple values inside of keys.
419.62 -> So this will be my next strategy.
422.45 -> I'm going to check if key is having a json
427.14 -> So json [dot] key is equal to keys.
434.85 -> So first whatever value we're store, we're going to store inside this key.
441.49 -> So first I'm going to check this one.
443.96 -> This condition,
444.96 -> If this condition satisfies then we are going to iterate all the json keys.
450.81 -> So let's say I'm going for claim type and so first i will check for this one.
458.001 -> this one then I will take with
459.08 -> this one like that i am going to iterate.
464.51 -> I'm going to iterate over this and I'm going to store as a next key
475.74 -> so it will show i have to cast as a string value.
479.55 -> So whatever keys I'm getting I'm casting into the string and
485.43 -> I'm storing inside the next key.
487.639 -> So whatever next keys I'm using i am going to store it.
491.43 -> Basically what i am doing?
493.71 -> I am checking if it is null then i am going inside that and using that keys- all the values
500.34 -> i am storing
501.73 -> inside this iterator first, then I'm iterating over the all the keys, SO it will go inside
508.389 -> first key,
509.389 -> second key, third key, fourth key like that it will go and then what i am doing
514.99 -> The key I'm storing into the next key.
517.76 -> First key i am syoting into next key and on top of that
521.18 -> I'm going to perform some operations.
523.34 -> So first i am writing code inside try catch block
528.89 -> json [dot] get next key , Important concept comes here
533.34 -> So what I'm going to do if this key consist a value of json object or json array
539.15 -> So first I'll take this key countries to do some key or just one area or do some object.
544.84 -> So I'm going to write json [dot] get next keys
550.74 -> Keys instance of json object- instance of json object and I write some code here
564.69 -> and in else condition what I'm going to do?
568.23 -> else if it is a json array if json [dot] next keys
579.18 -> instance of json array.
581.46 -> Then I'm going to perform another operation.
583.59 -> So first I'm checking value is available and then i will check if its a json object
590.88 -> or json array.
592.4 -> So if it is json object then i am going to perform this operation else i am going to
598.72 -> perform
599.72 -> else operation.
600.98 -> So this is first thing, now we are going to write some condition in if block.
605.46 -> If my key is json object, if it
609.14 -> exists is equal to true, sorry false.
613.32 -> If exists is false so inside also
617.1 -> that it is not available
622.529 -> then I'm going to make one recursive call and I go again I'm going to call this method
627.36 -> so get key and
630.79 -> I'm going to use this method, So here.
632.57 -> This time I'm going to pass my
638.86 -> next key and inside here what I'm going to do json [dot]
642.63 -> get json object- get json object and I have to pass next key.
648.59 -> So this will be my next key and this will be a actual key.
652.05 -> So inside this key whatever key I'm storing inside this key this whatever value I'm going
657.17 -> to find that
658.17 -> values I are available or not?
659.43 -> So here I'm going to make recursive call and we are going to check.
662.4 -> So if I'm run for this score it will work.
665.05 -> So first let me run this country so it will print us.
671.47 -> So it is going to print us.
673.46 -> So like this it will work.
674.57 -> So we are satisfied now.
676.26 -> If we are going to iterate entire json object then it will work.
682.68 -> Now we have to deal with json object inside
684.73 -> json array inside json object.
687.089 -> So the dynamic part of nested json, pure nested json.
691.3 -> json object handleded.
692.35 -> Now we have to handel this kind of situation.
696.05 -> ever be of use this kind of situation we have to handel.
700.11 -> Don't worry we are going to debug this code
703.16 -> and we will understand line by line, like how it works.
706.38 -> You need to bit explore recursive code as well.
708.589 -> How recursive code works?
710.35 -> So now what I'm going to do if it is the instance of json array then we habe to perform some
715.38 -> more operations.
717.91 -> So first thing is if it is a json object then using this next key then we have to convert
723.11 -> into a JSON
724.11 -> Array.
725.11 -> So we can write, json array json array is equal to json [dot]
734.05 -> get json array and we have to pass our next string.
738.089 -> So using this this value we can convert into string and then again we can loop through
744.589 -> and we can get one
745.589 -> by one values.
748.18 -> So for.
749.18 -> This will be my simple for loop.
750.79 -> So for int i is equal to zero
754.91 -> There is nothing is a rocket science.
756.73 -> science.
757.73 -> It is simpal way, i is equal to zero
759.27 -> array [dot] length
761.27 -> So we are iterating up to array [dot] length json array [dot] length and i plus plus
769.67 -> So this will be my first object.
771.76 -> Yeah.
772.76 -> So this is country key.
774.12 -> It is having one object, then it will be like this
778.06 -> It is having two object, then it will be like this
781.94 -> It will be like this, one object inside it is having another object
786.649 -> So we are going to do that.
788.57 -> So first I'm going to get the value.
791.94 -> So json array [dot] get first element - json array [dot] get array of i
798.529 -> So I'm going to get my first element then i am converting into tostring method
804.36 -> So it will be converted into a string and I'm storing as a string value
809.37 -> So this will be my json array string.
812.29 -> array string and reference type will be string value.
815.8 -> So using string again I'm going to check if it is just an object or not if it is simply
820.35 -> a plain string
821.58 -> then I can simply print, other wise i have to check if it is again json object
826.86 -> SO i have to go through this code
830.51 -> Again I'm going to convert it to json object
833.56 -> So this is again I'm going to use this line I'm putting here and instead of this input
841.73 -> this
842.73 -> will be my json input string and this will be my inner json
848.97 -> this will be my inner json
850.49 -> Inside inner json if it is false then I'm going to make one more recursive call and
856.82 -> it
857.82 -> is very important thing what you are going to pass.
859.91 -> So json key inside the inner json you have to pass inner json and key
863.8 -> key.
866.06 -> this key you have to pass here.
869.63 -> So I'm going to pass this key.
872.339 -> Now your code is done.
874.43 -> So this is how using this code you can parse all the types of the json.
882.08 -> Now let me going to run this and we are going to test with multiple values
889.67 -> So now I'm going to take this json.
893.29 -> So this is my first json and inside this string i am going to use this json.
902.63 -> So this is my first suggestion.
904.1 -> And what we are going to do first we are going to validate the ID.
907.59 -> So let's say I am taking example of ID.
910.07 -> Here I'm going to pass ID and I'm going to run my code
914.61 -> so I'm getting and so my use and let's say I'm going to take example of name.
923.46 -> So let's say I'm going to take a name key as a name and let me run my code.
934.37 -> yes I'm getting three names, one cup ice, one bag beans, one cup ice so again
939.85 -> one cup ice, one bag beans, one cup ice
942.56 -> So whatever value we can take let's say I'm going to take a category
945.37 -> category run as java application and again run.
955.29 -> Meal and meal so again both the categories having the same
957.41 -> values.
958.41 -> So that's what we are getting two values.
959.61 -> So we are already tested this json so now we are going to use this json.
965.33 -> Let me remove this.
967.76 -> inside this I I'm using this and we are going to value attribute two.
972 -> So it will print two times value two.
977.42 -> we are getting two tie value two
979.279 -> And we are going to use now complex one.
981.97 -> So let's say this is my complex json, i am putting here is a string.
986.55 -> We are going to validate the value of let's say first value which is status.
990.589 -> So it will print.
995.76 -> Okay
1000.42 -> so we are getting.
1001.72 -> Okay.
1002.74 -> Now we are going to hear, first we are going to get a question mark.
1008.05 -> So question mark we will get this entire value.
1011.91 -> This will be what is the key or the question mark if I'm going to use this run as a java
1023.88 -> application.
1025.16 -> the question mark key consist value as a another json object so that's why we are getting this
1033.01 -> value.
1034.01 -> This is first one, this is second one.
1037.189 -> And from here it is third one.
1040.12 -> Like this it will work but if you're going to get count will you or this value say this
1047.51 -> value again
1048.51 -> it is going to print this entire json object but if we're going to get a count so let's
1052.89 -> say your json is
1054.01 -> like different let's say some student application or some employee tree like that.
1061.54 -> number of each employee so you can get the phone number lets the discount will be a phone
1066.37 -> number so
1067.37 -> you can print easily count I'm going to run my code Yes I'm getting this count This is
1080.02 -> my
1082.28 -> first count so So will this count
1096.08 -> expand printing
1100.11 -> now let's say this is I have added one more inside this is discussed I really just one
1106.9 -> area inside again
1108.07 -> I have added one more json object and this is unique key
1111.36 -> So let's I'm going to add this one before that let's say I'm going to update this key
1117.28 -> value let's I'm
1118.28 -> going to update as a fundoo testers. and i am hing to use this.
1123.559 -> SO let me remove this, and i am going to grab this value
1131.11 -> so it will give me pass, string it will give me pass
1137.87 -> PASSwe are getting the same value so whatever.
1141.049 -> So how it is going it is going inside the first json array inside that json object inside
1148.5 -> the
1149.5 -> Sorry this is my json object, inside json array
1152.53 -> Json object
1153.85 -> json object is an object and here it is coming in this just an object it is coming here.
1161.309 -> This one.
1162.309 -> So these are going tojson array json object and whatever value so whatever it is I know
1167.85 -> how many
1168.85 -> times it is looping through our code is work now important part is.
1174.321 -> I understand there are some confusions here.
1177.74 -> So in this part we can help some confusion why I'm going to use a recursive code here.
1183.9 -> So again I'm going to call this method this method is going to call this method.
1187.87 -> So how it will work.
1189.64 -> So let's debug this code and we'll understand.
1193.84 -> So after that you will have a fair idea for understanding purpose.
1197.04 -> What I'm going to I'm going to use easy json so we can easily understand.
1201.48 -> We can easily grasp the thing.
1203.89 -> So I'm going to validate a attribute one inside this json.
1209.91 -> And I'm going inside.
1210.93 -> So the debugging mode.
1212.61 -> So I want to put my breakpoint first will be at if condition this else condition
1221.12 -> going to do here
1227.85 -> and how this line also work?
1229.25 -> So we'll loop through and let me
1233.2 -> Let me debug my code.
1234.29 -> I have to switch to debug mode.
1238 -> This is my debug mode
1239.48 -> and it has
1247.48 -> It is created here.
1248.85 -> First stack here.
1249.87 -> So this concept you have to understand very deeply if you had not followed this then you
1254.851 -> can not understand
1255.851 -> the code so first.
1257.83 -> Whenever I am going to execute this code my first breakpoint is here.
1261.51 -> So it comes here directly it comes here.
1264.14 -> And what it is saying the boolean value so exist it is false because outer array attribute
1270.47 -> one is not
1271.47 -> available outer array.
1272.89 -> So first it will check outside because outside we have only one key which is my object.
1279.96 -> So we don't have value so our json is
1283.63 -> This is my Jason if I'm going to inside the map so it is internally stored as a map because
1289.929 -> it is key
1290.929 -> and value.
1291.929 -> If you're not able to understand what this map
1293.72 -> is our map works how hashmap works internally you can watch that video.
1303.28 -> So this is is a key and value pair somewhat easier.
1305.35 -> My key is this key value is so this is my key and this is my value this is my value.
1317.03 -> And here we are going to further key as it is to the LTTE are and just it is sowing false.
1325.58 -> So it has created one hasmap value, in next key it store this object.
1332.11 -> Which is myobject
1333.11 -> outer Json I have only one value so what it will do.
1339.16 -> Let's say I'm going to do step over so it is simply it is going inside the Jason sorry
1344.04 -> it is not it is
1345.04 -> an object.
1346.04 -> So is this condition is not satisfied so it is going inside another.
1350.71 -> Else if condition let me do again.
1354.09 -> Step over.
1356.12 -> So again this condition is also this condition also not satisfied.
1360.58 -> So this again going to create one more thread here so you can see here you can if you are
1365.07 -> following this earlier
1366.07 -> it was two.
1367.07 -> Now it is it becomes three.
1369.01 -> So we are going to perform operation on this if we are going if you are going on this whole
1373.559 -> question
1374.559 -> we are here only now we have created this method is called a recursive function this
1379.25 -> method is again
1380.25 -> called this value and in this tech will you.
1385.15 -> Ah this one is full.
1386.89 -> Very helpful this one.
1389.21 -> What it can.
1390.82 -> It's pleated.
1391.82 -> Are just on to smaller a smaller piece.
1395.15 -> So if I'm going to this tech you can see my Jason values starting from the core.
1402.96 -> So now it is pleated from here.
1405.53 -> So in this Jason also.
1407.059 -> So first what we we had this one.
1410.2 -> Now we hope this design object.
1412.799 -> Got it.
1413.98 -> Now what it will do it will again going to perform the same prism.
1417.94 -> So let's say loose.
1420.03 -> I'll continue then it will split inside the one more Jason on.
1424.35 -> So again it is created when most big and now my Jason will be a little bit more to sort
1431.47 -> out.
1432.47 -> So this again started from this one.
1433.62 -> So now I hear only this really these three value in this deck.
1438.24 -> I am having only these three values so now I will get in this dress and I will get the
1444.9 -> value of our
1446.38 -> value.
1447.38 -> We we are going to get with you.
1449.51 -> So that's why I am here.
1451.49 -> And it will bring the value.
1452.98 -> So if I'm going to do a good value one and a next interest on this big work is known.
1462.539 -> So this take me anymore see five Lou against tape return here.
1468.539 -> This is gone.
1470.46 -> Now we are going to perform all present on this big so this take again we came here.
1477.99 -> It will go inside.
1480.93 -> It is getting the value in our string values.
1484.309 -> Again if we run this code.
1486.87 -> So a string really will be this string really.
1490.03 -> So again we're performing to person on this one because we'll perform the operation here
1498.86 -> Next up first
1499.86 -> We are here and then we did key event here then we're going here.
1506.83 -> So we have to take if I view one just object here now followed by Let's value another this
1515.11 -> object saw
1516.11 -> how it works.
1517.11 -> So that's why there are multiple stacks are created and we hope to complete the iteration
1528.2 -> in
1529.2 -> all the stacks.
1530.2 -> So this is how recursive code works.
1534.059 -> So now again it is going to take the condition and again it is going to go to key.
1538.15 -> So in this iteration so this condition is satisfied.
1542.9 -> So now my inner json or my Jason is this one.
1548.289 -> So with this will you do.
1549.94 -> This is for this one and we are getting one more value so it is again going to call this
1558.309 -> method
1559.76 -> and we can hear
1566.78 -> like this we are getting again value to another and for this stack also.
1572.82 -> So it is going to perform the same repetitive operation and this is our final stake.
1579.35 -> So if we are coming out of this loop then we will come here and our code is going to
1583.99 -> or so now are interesting
1586.049 -> is to if I'm going forward it is too late.
1590.47 -> So in this disconnected.
1593.3 -> So that's why in this Jason we are getting two value
1597.33 -> So don't worry guys if you're still not able to understand the code I'll push this code
1601.25 -> into the git hub.
1602.25 -> git hub..
1603.25 -> You can download the code and you can understand the stuff and you can debug yourself and still
1610.23 -> you
1611.23 -> are having the question you can put in the inside the comments section.
1613.62 -> I will be happy to answer those.
1616.13 -> Thank you guys.
1617.13 -> Thanks for watching the video.

Source: https://www.youtube.com/watch?v=ZjZqLUGCWxo