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

How to Convert String Value to JSON Object

discorapid

Co-Op Specialist
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
87
Likes
24
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. <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 String Value to JSON Object</

    h3

    >
  16. <hr

    style

    =

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

    /

    >
  17. <div

    class

    =

    "col-md-6"

    >
  18. <div

    class

    =

    "form-group"

    >
  19. <label

    >

    Firstname</

    label

    >
  20. <input

    type

    =

    "text"

    class

    =

    "form-control"

    id

    =

    "firstname"

    /

    >
  21. </

    div

    >
  22. <div

    class

    =

    "form-group"

    >
  23. <label

    >

    Lastname</

    label

    >
  24. <input

    type

    =

    "text"

    class

    =

    "form-control"

    id

    =

    "lastname"

    /

    >
  25. </

    div

    >
  26. <div

    class

    =

    "form-group"

    >
  27. <label

    >

    Address</

    label

    >
  28. <input

    type

    =

    "text"

    class

    =

    "form-control"

    id

    =

    "address"

    /

    >
  29. </

    div

    >
  30. <center><button

    onclick

    =

    "convertToJSON()"

    class

    =

    "btn btn-default"

    >

    Convert</

    button

    ></

    center>
  31. </

    div

    >
  32. <div

    class

    =

    "col-md-6"

    >

  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. function

    convertToJSON(

    )

    {
  4. const

    firstname =

    document.getElementById

    (

    'firstname'

    )

    ;
  5. const

    lastname =

    document.getElementById

    (

    'lastname'

    )

    ;
  6. const

    address =

    document.getElementById

    (

    'address'

    )

    ;

  7. if

    (

    firstname.value

    ==

    ""

    ||

    lastname.value

    ==

    ""

    ||

    address.value

    ==

    ""

    )

    {
  8. alert(

    "Please complete the required field!"

    )

    ;
  9. }

    else

    {
  10. const

    str =

    '{"firstname": "'

    +

    firstname.value

    +

    '", "lastname": "'

    +

    lastname.value

    +

    '", "address": "'

    +

    address.value

    +

    '"}'

    ;
  11. json[

    'member'

    ]

    .push

    (

    str)

    ;
  12. const

    decode =

    JSON.stringify

    (

    json,

    null

    ,

    ' '

    )

    ;
  13. const

    rpl =

    decode.replace

    (

    /\\/g

    ,

    ''

    )

    ;
  14. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    "<pre>"

    +

    rpl+

    "</pre>"

    ;
  15. firstname.value

    =

    ""

    ;
  16. lastname.value

    =

    ""

    ;
  17. address.value

    =

    ""

    ;
  18. }
  19. }

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:

How%20to%20Convert%20String%20Value%20to%20JSON%20Object%201.png


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.


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

442,401

317,942

317,951

Top