• We just launched and are currently in beta. Join us as we build and grow the community.

How to Convert HTML Inputs to JSON Object in JavaScript

Kaitama_kun

Page Speed Guru
K Rep
0
0
0
Rep
0
K Vouches
0
0
0
Vouches
0
Posts
30
Likes
198
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
How to Convert HTML Inputs to JSON Object in JavaScript

Introduction

In this tutorial we will create a How to Convert HTML Inputs to JSON Object in JavaScript. This tutorial purpose is to teach you how to convert your html inputs into JSON object. This will tackle all the basic function that will convert your form data. 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 can do it without a problem. This program can be use to any system or application if you want to convert your form data into a readable JSON object. I will give my best to provide you the easiest way of creating this program Convert form data into JSON Object. 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 display html form and JSON object. 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. <body

    >
  8. <nav

    class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav

    >
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    How to Convert HTML Inputs to JSON Object</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-8"

    >
  18. <h3

    >

    JSON RESULT</

    h3

    >
  19. <div

    id

    =

    "result"

    ></

    div

    >
  20. </

    div

    >
  21. <div

    class

    =

    "col-md-4"

    >
  22. <div

    class

    =

    "form-group"

    >
  23. <label

    >

    Firstname</

    label

    >
  24. <input

    type

    =

    "text"

    class

    =

    "form-control"

    id

    =

    "firstname"

    /

    >
  25. </

    div

    >
  26. <div

    class

    =

    "form-group"

    >
  27. <label

    >

    Lastname</

    label

    >
  28. <input

    type

    =

    "text"

    class

    =

    "form-control"

    id

    =

    "lastname"

    /

    >
  29. </

    div

    >
  30. <center><button

    onclick

    =

    "convertToJson()"

    class

    =

    "btn btn-primary"

    >

    Submit</

    button

    ></

    center>
  31. </

    div

    >
  32. </

    div

    >
  33. </

    body

    >
  34. <script

    src

    =

    "script.js"

    ></

    script

    >
  35. </

    html

    >

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically convert your html form data into JSON object when the 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. let member =

    JSON.parse

    (

    '{"members":[]}'

    )

    ;


  2. function

    displayJson(

    firstname,

    lastname,

    json)

    {
  3. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    "<pre>"

    +

    json+

    "</pre>"

    ;
  4. firstname.value

    =

    ""

    ;
  5. lastname.value

    =

    ""

    ;
  6. }


  7. function

    convertToJson(

    )

    {
  8. let firstname =

    document.getElementById

    (

    'firstname'

    )

    ;
  9. let lastname =

    document.getElementById

    (

    'lastname'

    )

    ;

  10. if

    (

    firstname.value

    ==

    ""

    ||

    lastname.value

    ==

    ""

    )

    {
  11. alert(

    "Please complete the required field!"

    )

    ;
  12. }

    else

    {
  13. let str =

    '{"firstname": "'

    +

    firstname.value

    +

    '", "lastname": "'

    +

    lastname.value

    +

    '"}'

    ;
  14. member[

    'members'

    ]

    .push

    (

    str)

    ;
  15. let stringify =

    JSON.stringify

    (

    member,

    null

    ,

    ' '

    )

    ;
  16. let json =

    stringify.replace

    (

    /\\/g

    ,

    ''

    )

    ;

  17. displayJson(

    firstname,

    lastname,

    json)

    ;
  18. }
  19. }

In the code above we create a method called convertToJson(), this function will convert the html form data into JSON object. We bind first the two input into variables then create a JSON structure with the two variables in it called str. We push the structure to array object and then we use JSON.stringify() function to make turn the array into JSON object.

Output:

How%20to%20Convert%20HTML%20Inputs%20to%20JSON%20Object%20in%20JavaScript%201.png


The How to Convert HTML Inputs to JSON Object in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Remove Element from an Array in JavaScript. 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


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

324,125

324,133

Top