Test your skills: JSON
This aim of this skill test is to assess whether you've understood our Working with JSON article.
Note: You can try out the solution in the interactive editor below. If you get stuck, then ask us for help — see the Assessment or further help section at the bottom of this page.
The one and only task in this article concerns accessing JSON data and using it in your page. JSON data about some mother cats and their kittens is available in sample.json. The JSON is loaded into the page as a text string and made available in the
catString parameter of the
displayCatInfo() function, called when the provided promise chain (which starts by fetching the JSON data) is fulfilled. Your task is to fill in the missing parts of the
displayCatInfo() function to store:
- The names of the three mother cats, separated by commas, in the
- The total number of kittens, and how many are male and female, in the
The values of these variables are then printed to the screen inside paragraphs.
- The JSON data is provided as text inside the
displayCatInfo()function. You'll need to parse it into JSON before you can get any data out of it.
- You'll probably want to use an outer loop to loop through the cats and add their names to the
motherInfovariable string, and an inner loop to loop through all the kittens, add up the total of all/male/female kittens, and add those details to the
- The last mother cat name should have an "and" before it, and a full stop after it. How do you make sure this can work, no matter how many cats are in the JSON?
- Why are the
para1.textContent = motherInfo;and
para2.textContent = kittenInfo;lines inside the
displayCatInfo()function, and not at the end of the script? This has something to do with async code.
Try updating the live code below to recreate the finished example:
<iframe>). If you put your code on CodePen for example, it would fail because the JSON it is requesting is not on the same origin. You can get around these restrictions using CORS, but this is beyond the scope of what we are teaching here.
You can practice these examples in the Interactive Editor above.
If you would like your work assessed, or are stuck and want to ask for help:
- Write a post asking for assessment and/or help at the MDN Discourse forum Learning category. Your post should include:
- A descriptive title such as "Assessment wanted for JSON skill test".
- Details of what you have already tried, and what you would like us to do, e.g. if you are stuck and need help, or want an assessment.
- Your code.
- A link to the actual task or assessment page, so we can find the question you want help with.