Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get JSON code from Textarea and parse it

I have a JSON code inside a textarea and I want to get it and work with it as it is exist in my website.

This is the textarea code :

<textarea>
  var settings = [
        {
            "id" : "post_thumbnail",
            "name" : "Default Post Thumbnail",
            "desc" : "The image src that you want to use for non-thumbnail posts.", 
            "type" : "text",
            "std" : "http://lorempixel.com/640/300/"
        }
    ]
</textarea>

I tried this but unfortunately it work only if the code exist in the website not in textarea because I get this error :

Uncaught ReferenceError: settings is not defined

for( var i = 0; i < settings.length; i++){

        var setting_id = settings[i].id,
            setting_name = settings[i].name,
            setting_desc = settings[i].desc,
            setting_type = settings[i].type,
            setting_std = settings[i].std;
$('body').html(setting_id +'<br>'+ setting_name +'<br>'+ setting_desc +'<br>'+ setting_type +'<br>'+  setting_std);
}

This is a live demo : http://jsfiddle.net/tauv0or1/

like image 1000
masked magician Avatar asked Aug 20 '15 14:08

masked magician


People also ask

How do I parse text in JSON?

Example - Parsing JSONUse the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.

What is JSON parse () method?

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.

What is the difference between JSON parse and JSON Stringify?

stringify() takes a JavaScript object and then transforms it into a JSON string. JSON. parse() takes a JSON string and then transforms it into a JavaScript object. Save this answer.


2 Answers

The value of textarea does not get parsed as JavaScript. Instead, change the value to valid JSON:

<textarea>
    [
        {
            "id" : "post_thumbnail",
            "name" : "Default Post Thumbnail",
            "desc" : "The image src that you want to use for non-thumbnail posts.", 
            "type" : "text",
            "std" : "http://lorempixel.com/640/300/"
        }
    ]
</textarea>

And tweak your JavaScript to use JSON.parse on the value:

var field = document.getElementById("the_textarea");
var settings = JSON.parse(field.value);

Because the user can enter arbitrary information into the textarea, you might want to wrap JSON.parse in a try-catch block, and alert the user to a syntax error:

var settings = null;

try {
    settings = JSON.parse(field.value);
}
catch (error) {
    if (error instanceof SyntaxError) {
        alert("There was a syntax error. Please correct it and try again: " + error.message);
    }
    else {
        throw error;
    }
}
like image 197
Greg Burghardt Avatar answered Sep 28 '22 08:09

Greg Burghardt


You need to eval the text first to actually run it as javascript:

eval($('textarea').text());

http://jsfiddle.net/tauv0or1/1/

Alternatively, you can parse it as JSON, but you need to edit the contents of the textarea first:

var settings = JSON.parse($('textarea').text());

http://jsfiddle.net/tauv0or1/2/

like image 44
James Brierley Avatar answered Sep 28 '22 10:09

James Brierley