What Is a JSON File? A Beginner’s Guide With JavaScript Examples
A JSON file is a plain text file that stores structured data using the JSON (JavaScript Object Notation) format. It usually has a .json extension and is commonly used for configuration files, APIs, data exports, and communication between a website and a server.
What JSON looks like
JSON stores data as key-value pairs and arrays. Keys are always wrapped in double quotes, values are separated from keys with colons, and multiple pairs are separated by commas.
In this example:
"name","email", and"age"are keys."Sarah","sarah@example.com", and29are values."skills"is an array."address"is a nested object.trueis a boolean value.
JSON supports strings, numbers, booleans, arrays, objects, and null. It does not support comments.
JSON vs JavaScript objects
JSON looks similar to JavaScript objects, but it is text-based and has stricter rules. For example, JSON keys must use double quotes, while JavaScript object keys do not have to.
A JSON file is simply a text file saved with the .json extension. Its standard MIME type is application/json.
Convert JSON to JavaScript
Use JSON.parse() to turn a JSON string into a JavaScript object or array.
Demo
Output
Convert JavaScript to JSON
Use JSON.stringify() to convert a JavaScript object into a JSON string. This is useful when sending data to an API or saving it in a file.
Output
The 2 adds two-space indentation, making the JSON easier to read.
Load a JSON file with JavaScript
You can load a JSON file in the browser using the Fetch API. The response.json() method reads the response and converts the JSON into a JavaScript value.
Create a file called users.json:
Then load and display it:
Output:
Read a local JSON file
If you want users to upload a JSON file, use the File Reader API.
This lets a visitor select a .json file, validates it, and displays its formatted contents.
Send JSON to a server
JSON is widely used when submitting data to APIs. Convert your JavaScript object to JSON with JSON.stringify(), then send it with fetch().
The Content-Type: application/json header tells the server that the request body contains JSON.
Handle JSON errors safely
Invalid JSON will cause JSON.parse() to throw an error. Always use try...catch when parsing JSON from user input, files, or external sources.
The example above fails because JSON does not allow a trailing comma after "age": 29.
Common uses for JSON files
- APIs: Send and receive structured data between a browser, mobile app, or server.
- Configuration files: Store settings such as database details, feature flags, and build options.
- Website content: Store product details, FAQs, testimonials, or blog settings in a structured format.
- JavaScript projects: Files such as
package.jsonstore project information and dependencies. - WordPress block themes: Modern WordPress themes can use
theme.jsonto define global styles, colours, typography, and spacing. - Data imports and exports: Move records between systems in a readable, portable format.
Quick JSON rules
- Use double quotes for keys and string values.
- Separate key-value pairs with commas.
- Do not add a comma after the final item.
- Use curly braces
{}for objects. - Use square brackets
[]for arrays. - Do not include comments in a JSON file.
JSON is popular because it is lightweight, readable, and supported by almost every programming language.