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

Convert String To JSON Using JavaScript

Arieloos

Virtual Reality Pioneer
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
76
Likes
139
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
The code we will tackle about is Convert String To JSON using JavaScript. The program will convert any string that you enter in to a readable JSON object. The trick within this code is that you need to push all the string into an array object, then use JSON.stringtify() in order to safely display the JSON object as a string. To learn more about this, just follow the steps below.

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this 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"

    >

    Convert String To JSON Using JavaScript</

    h3

    >
  16. <hr

    style

    =

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

    /

    >
  17. <div

    class

    =

    "col-md-8"

    >
  18. <h3

    >

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

    class

    =

    "form-group"

    >
  31. <label

    >

    Address</

    label

    >
  32. <input

    type

    =

    "text"

    class

    =

    "form-control"

    id

    =

    "address"

    /

    >
  33. </

    div

    >
  34. <center

    ><button

    onclick

    =

    "convertString()"

    class

    =

    "btn btn-primary"

    >

    Convert</

    button

    ></

    center

    >
  35. </

    div

    >

  36. </

    div

    >
  37. </

    body

    >
  38. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  39. </

    html

    >

Creating the Script

This code contains the script of the application. The code will forcefully convert the string into a JSON object. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. var

    member =

    JSON.parse

    (

    '{"member":[]}'

    )

    ;

  2. function

    displayResult(

    firstname,

    lastname,

    address,

    json)

    {
  3. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    "<pre>"

    +

    json+

    "</pre>"

    ;
  4. firstname.value

    =

    ""

    ;
  5. lastname.value

    =

    ""

    ;
  6. address.value

    =

    ""

    ;
  7. }


  8. function

    convertString(

    )

    {
  9. var

    firstname =

    document.getElementById

    (

    'firstname'

    )

    ;
  10. var

    lastname =

    document.getElementById

    (

    'lastname'

    )

    ;
  11. var

    address =

    document.getElementById

    (

    'address'

    )

    ;

  12. if

    (

    firstname.value

    ==

    ""

    ||

    lastname.value

    ==

    ""

    ||

    address.value

    ==

    ""

    )

    {
  13. alert(

    "Please complete the required field!"

    )

    ;
  14. }

    else

    {
  15. var

    str =

    '{"firstname": "'

    +

    firstname.value

    +

    '", "lastname": "'

    +

    lastname.value

    +

    '", "address": "'

    +

    address.value

    +

    '"}'

    ;
  16. member[

    'member'

    ]

    .push

    (

    str)

    ;
  17. var

    stringify =

    JSON.stringify

    (

    member,

    null

    ,

    ' '

    )

    ;
  18. var

    json =

    stringify.replace

    (

    /\\/g

    ,

    ''

    )

    ;

  19. displayResult(

    firstname,

    lastname,

    address,

    json)

    ;
  20. }
  21. }

There you have it we successfully created a Convert String To JSON using 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!


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

442,401

317,942

317,951

Top