How to Convert String Value to JSON Object

Language

Introduction

In this tutorial we will create a How to Convert String Value to JSON Object. This tutorial purpose is to provide you way to convert your html string into JSON object. This will tackle the conversion and appending string to a JSON object. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you will do it without a problem. This program is useful if you have some data that your want to be store as a JSON. I will try my best to give you the easiest way of creating this program String value to JSON conversion. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will a HTML form that we will be using to conversion. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">How to Convert String Value to JSON Object</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-6">
  17. <div class="form-group">
  18. <label>Firstname</label>
  19. <input type="text" class="form-control" id="firstname"/>
  20. </div>
  21. <div class="form-group">
  22. <label>Lastname</label>
  23. <input type="text" class="form-control" id="lastname"/>
  24. </div>
  25. <div class="form-group">
  26. <label>Address</label>
  27. <input type="text" class="form-control" id="address"/>
  28. </div>
  29. <center><button onclick="convertToJSON()" class="btn btn-default">Convert</button></center>
  30. </div>
  31. <div class="col-md-6">
  32.  
  33. <h4>Result</h4>
  34. <div id="result"></div>
  35. </div>
  36. </div>
  37. </body>
  38. <script src="script.js"></script>
  39. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will convert all the string value into JSON object button is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. const obj = '{"member":[]}';
  2. const json = JSON.parse(obj);
  3.  
  4. function convertToJSON(){
  5. const firstname = document.getElementById('firstname');
  6. const lastname = document.getElementById('lastname');
  7. const address = document.getElementById('address');
  8.  
  9. if(firstname.value == "" || lastname.value == "" || address.value == ""){
  10. alert("Please complete the required field!");
  11. }else{
  12. const str = '{"firstname": "'+firstname.value+'", "lastname": "'+lastname.value+'", "address": "'+address.value+'"}';
  13. json['member'].push(str);
  14. const decode = JSON.stringify(json, null, ' ');
  15. const rpl = decode.replace(/\\/g, '');
  16. document.getElementById('result').innerHTML = "<pre>"+rpl+"</pre>";
  17. firstname.value = "";
  18. lastname.value = "";
  19. address.value = "";
  20. }
  21. }

In this code we first create two variables called obj and json, in objwe create json string then parse the string into a json object. After that we created a method that will hold a certain functions. We first bind the form inputs and then contain them into a variable called str. Next we push all the string into the json object, and then use json.stringify().

Output:

The How to Convert String Value to JSON Object source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Convert String Value to JSON Object. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment