Complete 2024 Web Development Bootcamp

Dr. Angela Yu

Back to JSON Index


JSON Files
What Are JSON Files

JSON is an acronym for "JavaScript Object Notation". JSON files are text files that store data in a structured way that can be easily transmitted and used. JSON files are a versatile and widely adopted format for data interchange, offering simplicity, readability, and ease of use.

They are widely used for transmitting data between web applications and servers. JSON files use a simple syntax that consists of key-value pairs and arrays. They can contain various data types, such as strings, numbers, booleans, arrays, and objects. For example, a JSON file might look like this:

  • { "firstName": "John", "lastName": "Smith", "isAlive": true, "age": 27, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100" }, "phoneNumbers": [ { "type": "home", "number": "212 555-1234" }, { "type": "office", "number": "646 555-4567" } ], "children": [], "spouse": null }

JSON files are commonly used in web development, particularly in APIs, where they facilitate the exchange of data between clients and servers. They are also used in configuration files, data storage, and data sharing between different systems.


JSON Stringify()

So json files are a great way to transfer data, but how are json files created? Say we have a JavaScript Object we want to transfer, how do we turn it into a jason file?

We can convert the JavaScript data into a json file by using the JSON.stringify() method. This converts the data into a json string. It serializes the object into a string in JSON format.

The method can take up to three arguments: the value to convert, a replacer function or array, and a space parameter for formatting.

More Info:


JSON Parse()

And to convert a json string into a useable JavaScript object, we use the JSON.parse() method. This method takes a JSON string as input and returns the corresponding JavaScript value or object.

The syntax for JSON.parse() is:

  • JSON.parse( text ) or
  • JSON.parse( text, reviver )

The first parameter, text, is the JSON string to be parsed. The second parameter, reviver, is an optional function that can be used to transform the parsed value before it is returned.

More Info:


Back to Top