Raunak Gupta
Raunak Gupta

Reputation: 11

Convert JavaScript variable string to JSON using JavaScript

I have a string in JavaScript I want to make some kind of JSON of key value pair. Here is string

 {
    "questionId": null,
    "articleId": null,
    "questionText": "eewrr",
    "pollType": null,
    "selectedOptionIds": [],
    "pollOptionList": [{
        "optionId": 0,
        "questionId": 0,
        "optionText": "werwer",
        "selectedByUser": "N",
        "createdDate": null,
        "modifiedDate": null
    }, {
        "optionId": 0,
        "questionId": 0,
        "optionText": "werwer",
        "selectedByUser": "N",
        "createdDate": null,
        "modifiedDate": null
    }, {
        "optionId": 0,
        "questionId": 0,
        "optionText": "werwer",
        "selectedByUser": "N",
        "createdDate": null,
        "modifiedDate": null
    }, {
        "optionId": 0,
        "questionId": 0,
        "optionText": "werwe",
        "selectedByUser": "N",
        "createdDate": null,
        "modifiedDate": null
    }, {
        "optionId": 0,
        "questionId": 0,
        "optionText": "rwer",
        "selectedByUser": "N",
        "createdDate": null,
        "modifiedDate": null
    }],
    "pollId": 37
}

Upvotes: 0

Views: 57

Answers (5)

jitendra varshney
jitendra varshney

Reputation: 3562

use JSON.parse() for converting string to json and JSON.stringify() for converting json to string

Upvotes: 0

Mark Slingerland
Mark Slingerland

Reputation: 21

You're going to want to parse it as an JSON Object So it's going to be:

JSON.parse('{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}', (key, value));

In your case it will be:

JSON.parse('{"questionId":null,"articleId":null,"questionText":"eewrr","pollType":null,"selectedOptionIds":[],"pollOptionList":[{"optionId":0,"questionId":0,"optionText":"werwer","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"werwer","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"werwer","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"werwe","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"rwer","selectedByUser":"N","createdDate":null,"modifiedDate":null}],"pollId":37}', key, value));

I hope I have provided you with enough information

Upvotes: 0

Harsh Barach
Harsh Barach

Reputation: 947

var your_str = '{"questionId":null,"articleId":null,"questionText":"eewrr","pollType":null,"selectedOptionIds":[],"pollOptionList":[{"optionId":0,"questionId":0,"optionText":"werwer","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"werwer","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"werwer","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"werwe","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"rwer","selectedByUser":"N","createdDate":null,"modifiedDate":null}],"pollId":37}';
var obj = JSON.parse(your_str);
alert(obj);
//or
console.log(obj);

Here your_str is your json string.

Upvotes: 0

Curiousdev
Curiousdev

Reputation: 5778

You can use JSON.parse() please find below snippet for more information

var json = JSON.parse('{"questionId":null,"articleId":null,"questionText":"eewrr","pollType":null,"selectedOptionIds":[],"pollOptionList":[{"optionId":0,"questionId":0,"optionText":"werwer","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"werwer","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"werwer","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"werwe","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"rwer","selectedByUser":"N","createdDate":null,"modifiedDate":null}],"pollId":37}') 
console.log(json);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

geekbro
geekbro

Reputation: 1323

for that purpose use

   JSON.parse()

Upvotes: 0

Related Questions