JavaScript Object Notation (JSON) is a lightweight data interchange format. It is based on JavaScripts object literal notation.
Although JSON is a subset of JavaScript, it is language independent. It can be used to exchange data between programs written in all modern programming languages. It is a text format, so it is readable by both humans and machines. It is easy to implement and easy to use.
JavaScript Object Notation (JSON) is an open standard developed by Douglas Crockford, author
Prior to the JSON specification, most client-server communications were being delivered in much more verbose XML snippets. JavaScript developers often work with JSON web services and frequently define internal data using JSON syntax.
A JSON object is an unordered container of name/value pairs. A name can be any string. A value can be any JSON value, including arrays and objects. JSON objects can be nested to any depth, but generally it is most effective to keep them relatively flat. Most languages have a feature that maps easily to JSON objects, such as an object, struct, record, dictionary, hash table, property list or associative array.
The JSON array is an ordered sequence of values. A value can be any JSON value, including arrays and objects. A value can be any JSON value, including arrays and objects. Most languages have a feature that maps easily onto JSON arrays, such as arrays, vector, list or sequence.
Although JSON is language independent, I'll be using Node.js in my examples, however they should work within any JavaScript framework.
Creating a JSON Object
A JSON object in Node.js uses {} syntax to declare the JSON data type. In our initial example we'll create a simple JSON object to store details about a freelancer.
We can extend this object to include a nested JSON object to include address information.
Displaying Data
After we have created our JSON object we can print the contents out to console, by simply using console.log. This will show a native JSON object in the console.
Accessing Data
If you want to get a specific attribute of a JSON object, then you can call the attribute name directly.
If you call an unknown attribute of a JSON object, you will get an undefined value.
You can iterate through your JSON object to retrieve what attributes have been defined.
If you want to check whether a property has been defined on a JSO object you can use the hasOwnProperty function. This is a boolean function so will return either true or false.
Editing Data
Editing JSON data can be performed by assigning new values to the object's attribute.
You can also change a value by accessing the object like an array using the []
JSON Array
We can also combine our object to include a collection of values.
If you want to access a JSON array, for instance, using the details attribute, you can pass the index parameter to get a single JSON object.
- How to add RSS feed to nuxt 3 - September 25, 2023
- What is Middleware in Dotnet - September 19, 2023
- How to create a WebSocket server in dotnet - September 11, 2023