fetch data from different JSON files at the same time

14 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€66.52
List Price:  €95.03
You save:  €28.51
£55.29
List Price:  £78.99
You save:  £23.70
CA$98.01
List Price:  CA$140.02
You save:  CA$42.01
A$108.26
List Price:  A$154.67
You save:  A$46.40
S$94.28
List Price:  S$134.69
You save:  S$40.41
HK$544.68
List Price:  HK$778.14
You save:  HK$233.46
CHF 62.24
List Price:  CHF 88.92
You save:  CHF 26.68
NOK kr781.73
List Price:  NOK kr1,116.81
You save:  NOK kr335.07
DKK kr496.25
List Price:  DKK kr708.95
You save:  DKK kr212.70
NZ$119.48
List Price:  NZ$170.70
You save:  NZ$51.21
د.إ257.07
List Price:  د.إ367.25
You save:  د.إ110.18
৳8,430.24
List Price:  ৳12,043.71
You save:  ৳3,613.47
₹5,907.88
List Price:  ₹8,440.19
You save:  ₹2,532.31
RM313.59
List Price:  RM448
You save:  RM134.41
₦117,896.75
List Price:  ₦168,431.15
You save:  ₦50,534.40
₨19,601.13
List Price:  ₨28,002.81
You save:  ₨8,401.68
฿2,456.64
List Price:  ฿3,509.64
You save:  ฿1,053
₺2,405.01
List Price:  ₺3,435.88
You save:  ₺1,030.86
B$408.16
List Price:  B$583.12
You save:  B$174.95
R1,278.38
List Price:  R1,826.34
You save:  R547.95
Лв129.87
List Price:  Лв185.54
You save:  Лв55.66
₩98,547.64
List Price:  ₩140,788.38
You save:  ₩42,240.74
₪262.83
List Price:  ₪375.48
You save:  ₪112.65
₱4,119.38
List Price:  ₱5,885.08
You save:  ₱1,765.70
¥10,926.10
List Price:  ¥15,609.38
You save:  ¥4,683.28
MX$1,438.75
List Price:  MX$2,055.45
You save:  MX$616.69
QR257.25
List Price:  QR367.52
You save:  QR110.26
P957.42
List Price:  P1,367.80
You save:  P410.38
KSh9,063.70
List Price:  KSh12,948.70
You save:  KSh3,885
E£3,455.33
List Price:  E£4,936.40
You save:  E£1,481.07
ብር8,611.91
List Price:  ብር12,303.26
You save:  ብር3,691.35
Kz63,807.12
List Price:  Kz91,156.94
You save:  Kz27,349.81
CLP$68,583.90
List Price:  CLP$97,981.20
You save:  CLP$29,397.30
CN¥506.99
List Price:  CN¥724.30
You save:  CN¥217.31
RD$4,249.83
List Price:  RD$6,071.45
You save:  RD$1,821.61
DA9,380.49
List Price:  DA13,401.28
You save:  DA4,020.78
FJ$159.39
List Price:  FJ$227.72
You save:  FJ$68.32
Q545.12
List Price:  Q778.78
You save:  Q233.65
GY$14,759.55
List Price:  GY$21,085.97
You save:  GY$6,326.42
ISK kr9,759.40
List Price:  ISK kr13,942.60
You save:  ISK kr4,183.20
DH701.60
List Price:  DH1,002.33
You save:  DH300.73
L1,265.07
List Price:  L1,807.32
You save:  L542.25
ден4,092.25
List Price:  ден5,846.32
You save:  ден1,754.07
MOP$565.22
List Price:  MOP$807.49
You save:  MOP$242.27
N$1,267.59
List Price:  N$1,810.92
You save:  N$543.33
C$2,596.85
List Price:  C$3,709.94
You save:  C$1,113.09
रु9,521.24
List Price:  रु13,602.36
You save:  रु4,081.11
S/267.43
List Price:  S/382.06
You save:  S/114.63
K283.56
List Price:  K405.11
You save:  K121.54
SAR262.97
List Price:  SAR375.69
You save:  SAR112.72
ZK1,929.77
List Price:  ZK2,756.93
You save:  ZK827.16
L331.05
List Price:  L472.96
You save:  L141.90
Kč1,682
List Price:  Kč2,402.96
You save:  Kč720.96
Ft27,138
List Price:  Ft38,770.23
You save:  Ft11,632.23
SEK kr772.63
List Price:  SEK kr1,103.81
You save:  SEK kr331.17
ARS$69,869.10
List Price:  ARS$99,817.28
You save:  ARS$29,948.18
Bs487.60
List Price:  Bs696.60
You save:  Bs209
COP$315,227.33
List Price:  COP$450,344.06
You save:  COP$135,116.73
₡36,031.09
List Price:  ₡51,475.19
You save:  ₡15,444.10
L1,781.45
List Price:  L2,545.04
You save:  L763.59
₲550,680.18
List Price:  ₲786,719.69
You save:  ₲236,039.51
$U2,973.65
List Price:  $U4,248.26
You save:  $U1,274.60
zł288.46
List Price:  zł412.11
You save:  zł123.64
Already have an account? Log In

Transcript

The only thing new in this example is how to access to more than one JSON file. So do you see so this is the first JSON file, and here is the second one. So now when we click the button, one time, the data for the first JSON file gets played this one, which has a name jack and his age and his profession. And when we click, it's time the button will be disappeared. And the data for a second yes and file will be displayed. So it contains a name for someone called Joey and someone called Adam and his age and his proficiency to hearing aid.

So let me see how we can do this. The to start from scratch. So I removed everything. We can Tag. So firstly we define two variables not to us to the input, which is the button and another one to access to the division that will hold the text or the data variable and the first one called etn. You too so now we can make a function for the button.

So btn select go to function and look at this function, we will look in the HTTP request. So, define a variable called you It will show no XML HTTP. So let's hop in. And then the method which is yet and then the name of the Jason file, but which one we will choose that we just Now type the first JSON file, which you call file on. And I will show you later how we can access to the second one. file, one dot JSON, then it's that on load, go to a function guardian.

Will y equal to JSON dot parse? Nothing till now is new. Everything we have explained before, I told you that the new thing is how to access to more than one file. So the goal that will access to the data inside the JSON file will be on another function, and we will just call it here. So it's named as render HTML. It would take white which hold now that Jason data, finally liquid send the request.

So it's that send. Now this will make the function My name is Brenda cheeky l Learn as an argument data. Firstly to make the loop that will loop through all the data inside each JSON time for variable or equal to zero, and then data dot length i plus plus. But we forgot to define a variable to be updated with the loop. So fixed and equal to empty. So within a row, that text will be equal to the old text plus, which will display the name and age So, but let's we'll make all this inside of paragraph 10 Plus is played age.

They'll make another loop to loop through the array here in the first and the second JSON file. Each one of them has an array for work object. Within this lobe, it will display the work. But you see here that the work for here with the first object, just one value and here are two values. So, we have to make an if condition to check if it has one value, so display this value and if it has two values, so, so display the first value, and then check if there is another value, add this value and add another text. So let me see.

Yes, equal zero. So in this case, will equal to x plus data on the work But let's we add text here and work as deals your stand so now just to remain to display this data inside the division that she didn't know the filament that served an event insert adjacent HTML So this even take the position and the variable that we want to display. So I want to explain before and, and the variable is fixed for sure. Which you know, hold all the data within the JSON file. So now we expect that that data that will be appear in the data of the JSON file one. So there's something wrong with chicken syntax error line 26 Rule 26 here HTML and that insert adjacent is not a function.

So there will be with the name of the function, insert adjacent HTML or one of the inputs. So to check this out, you're served. And text is a variable. So tests p without any codes, you say. Nice. So here is the data for the first JSON file, which is for what someone called jack this one Now we want to display the second year some file.

So how we can do this. So we define variable and call it flag. And it will be equal to one, then increment this flag when we click the button, so now when we click the button, select will be equal to two. And here change the name of the JSON file. So it'll be a file plus this flag. So now flag is equal to one.

So it will open file one dot Jason, which is the first JSON file. Then the next clip will be file to the Jason so it will be the second file, the two Chiklis So here's the wrong yeah here. So here is the data for the first JSON file, jack, the little click again. So here is the data for the second JSON file for someone called you and Adam. Nice. So that's how you can make a loan to display more than one JSON file.

Just now remain to remove the button once we click the second time. So if flag is more than two so in this case, button, the style Display the liquid tool number nine nice to add another trick. They want to make this division content in the wall. So there was a candidate in the data. So an attribute content is a one people to show that you save, see, but because we make content available, so the background for the division displayed before we click the resolve this it's too easy To give it a style, we're to display none and then change it here when we click the button. So, HTML no style display, it'll be a virtual look.

So now those are can change anything. Like for example here, work as an English teacher. Nice. So that's everything about how you can access to more than one JSON file. I hope you enjoyed this exercise and see you next time.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.