Answers for "javascript json from string"

9

change js to json

var obj = {name: "Martin", age: 30, country: "United States"};
 
// Converting JS object to JSON string
var json = JSON.stringify(obj);
 
console.log(json);
// Prints: {"name":"Martin","age":30,"country":"United States"} 
"https://www.tutorialrepublic.com/faq/how-to-convert-js-object-to-json-string.php"
Posted by: Guest on May-16-2020
41

Javascript object to JSON string

var person={"first_name":"Tony","last_name":"Hawk","age":31};
var personJSONString=JSON.stringify(person);
Posted by: Guest on July-23-2019
2

string into json javascript

JSON.stringify(obj);
Posted by: Guest on July-14-2021
6

js string to json

var obj = JSON.parse("{no:'u',my:'sql'}");//returnes {no:'u',my:'sql'}
Posted by: Guest on April-08-2020
0

javascript extract json from string

function extractJSON(str) {
    var firstOpen, firstClose, candidate;
    firstOpen = str.indexOf('{', firstOpen + 1);
    do {
        firstClose = str.lastIndexOf('}');
        console.log('firstOpen: ' + firstOpen, 'firstClose: ' + firstClose);
        if(firstClose <= firstOpen) {
            return null;
        }
        do {
            candidate = str.substring(firstOpen, firstClose + 1);
            console.log('candidate: ' + candidate);
            try {
                var res = JSON.parse(candidate);
                console.log('...found');
                return [res, firstOpen, firstClose + 1];
            }
            catch(e) {
                console.log('...failed');
            }
            firstClose = str.substr(0, firstClose).lastIndexOf('}');
        } while(firstClose > firstOpen);
        firstOpen = str.indexOf('{', firstOpen + 1);
    } while(firstOpen != -1);
}

var obj = {'foo': 'bar', xxx: '} me[ow]'};
var str = 'blah blah { not {json but here is json: ' + JSON.stringify(obj) + ' and here we have stuff that is } really } not ] json }} at all';
var result = extractJSON(str);
console.log('extracted object:', result[0]);
console.log('expected object :', obj);
console.log('did it work     ?', JSON.stringify(result[0]) == JSON.stringify(obj) ? 'yes!' : 'no');
console.log('surrounding str :', str.substr(0, result[1]) + '<JSON>' + str.substr(result[2]));
Posted by: Guest on June-11-2021

Code answers related to "javascript json from string"

Code answers related to "Javascript"

Browse Popular Code Answers by Language