Data in JSON is only text until a script turns it into an object. The Fetch API then provides a modern way to get that text from a server.
Parse and Stringify
The method JSON.parse(text) converts JSON text into a JavaScript object. If the text is not valid JSON, it throws an error. The method JSON.stringify(object) does the opposite: it converts an object into JSON text, ready to send to a server or store. Together they move data between a program and the outside world.
Promises
A promise is an object that stands for a result that will arrive later. It ends either as a success or as a failure. The method then supplies code for the success, and catch supplies code for a failure.
Definition: Promise. An object that stands for a result that will arrive later, as a success or a failure.
Using fetch
The Fetch API is a modern interface for requesting data. The call fetch(url) returns a promise. The first then receives a response object, and response.json() reads its body as JSON. A second then receives the data itself. One important point is that fetch does not fail on a 404 or 500 reply. It fails only when the network breaks. Code should therefore check response.ok before reading the data.
Definition: Fetch API. A modern browser interface that requests data from a server and returns a promise.
Table 4.8: XMLHttpRequest compared with fetch
| Aspect | XMLHttpRequest | fetch |
|---|---|---|
| Style | Events and callbacks | Promises |
| Code length | Longer | Shorter |
| JSON reading | JSON.parse(responseText) | response.json() |
| Status check | status === 200 | response.ok |
Showing the Data
Once the data is an array, a loop can build a table row or a list item for each record with createElement, and appendChild adds it to the page. An empty result should show a clear message.
Example 4.8: Stringify, parse and a fetch call
const student = { name: "Asha", marks: [82, 67] };
const text = JSON.stringify(student);
console.log(text);
const copy = JSON.parse(text);
console.log(copy.marks[0] + copy.marks[1]);
Output:
{"name":"Asha","marks":[82,67]}
149
In a page, the same data would be fetched from a file:
<script>
fetch("students.json")
.then(r => {
if (!r.ok) throw new Error(r.status);
return r.json();
})
.then(list => list.forEach(s => console.log(s.name)))
.catch(err => console.log("Failed: " + err.message));
</script>
Common mistakes
- Forgetting to check response.ok, since fetch does not fail on a 404.
- Forgetting to return response.json() in the first then, so the next step gets undefined.
- Calling JSON.parse on text that is already an object.
- Opening the page from the file system, where fetch to a local file is blocked.
Key takeaways
- JSON.parse turns text into an object, and JSON.stringify does the reverse.
- fetch returns a promise, and then and catch handle success and failure.
- Check response.ok, because fetch does not reject on a 404 or 500 reply.
- Build the page from the data with createElement and appendChild.
Practice questions
- What does JSON.stringify do?
- Does fetch fail on a 404 reply?
- What is a promise?
Answers to the practice questions
- It converts a JavaScript object into JSON text.
- No. It fails only on a network error, so response.ok must be checked.
- An object that stands for a result that will arrive later, as a success or a failure.
Sources
- Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit IV
