All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Create JSON Object In JavaScript Dynamically

Last Updated : Mar 11, 2024

Create JSON Object In JavaScript Dynamically

In this article we will show you the solution of create JSON object in JavaScript dynamically, let us understand JSON first. The JSON stands for JavaScript object notation.

It is a lightweight data-exchanging format. It is used basically with API. JSON is language-independent.

Step By Step Guide On Create JSON Object In JavaScript Dynamically :-

In JSON, we often use two functions: - JSON.stringify and JSON.parse.

JSON.stringify - This function is used to convert corresponding objects to strings or stringify them.

Example :

const obj = { name: " Thomas ", country : " japan " };
const JSON = JSON.stringify( obj );

JSON.parse - this function is used to convert a string into a corresponding JSON object or JavaScript value.

Example :

const json = '{"name":Peter, "country":England};
const obj = JSON.parse( json );
<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>create JSON object in JavaScript dynamically</title>
        var jsonData = {} ;
        document.write ( " JsonData : " + JSON.stringify(jsonData)) ;
        // to create JSON object = "thomas" ;
        jsonData.age = 20 ; "london";
        document.write ( " Data with details: " +JSON.stringify(jsonData) );
        // to update JSON object = "Peter" ;
        jsonData.age = 40 ; = "seoul" ;
        document.write ("upadated data : " + JSON.stringify(jsonData) ) ;
         //to read JSON object
         document.write ( "the Data: " ) ;
         document.write ( "name: " + ) ;
         document.write ( "age: " + jsonData.age );
         //to delete JSON object
         delete jsonData.age ;
         delete ;
         document.write("Data after delete : " + JSON.stringify(jsonData) );
  1. First, we write <! DOCTYPE HTML > which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the < HTML > tag is used to indicate the beginning of an HTML document.
  3. As above now the < head > tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both < head > and < title > tags are Paired tags. So, both have < /head > and < /title > ending tags respectively.
  5. Thirdly, the < body > tag is used to define the webpage body. All the contents to show on the website are written here.
  6. Then attach JavaScript by using the < script > tag into the < head > tag.
  7. First create JSON data ( ex: - name, age, city, etc.) and then convert it into a string by using JSON.stringify();
  8. We can update the previously written JSON data by using JSON.stringify(); again.
  9. We can read the data using document.write().
  10. Lastly using the delete function we can delete any object into the string. Ex: - delete;.

Conclusion :-

Lastly, in conclusion, here we can say that with the help of this article create JSON objects in JavaScript dynamically.

I hope this article on create JSON object in JavaScript dynamically helps you and the steps and method mentioned above are easy to follow and implement.