Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6074551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:23:41+00:00 2026-05-23T10:23:41+00:00

Im looking at another developers code that parses some JSON. I cant really see

  • 0

Im looking at another developers code that parses some JSON. I cant really see how it works or if its a good idea…

hmlPlaylist.prototype.loadVideos_callback = function (data) {
    var jsonData = '';
    var jsonError = false;

    try {
        jsonData = eval("(" + data + ")");
    } catch (jError) {
        jsonError = true;
    }

    if (!jsonError) {

        if (jsonData.playlists.length > 0) {
            this.buildPlaylistList(jsonData.playlists);
        }
        if (jsonData.videos.length > 0) {
            this.buildVideoList(jsonData.videos);
            this.bindVideoNavs();
        }
    }
    else {

        // no json returned, don't do anything
    }
};

Its the line

jsonData = eval("(" + data + ")");

That gets me. What does this do to the data? I thought you had to use an extarnal class library to parse JSON data?

I can see later on that he iterates the collection like this:

for (var i = 0; i < playlistCount; i++) {

        var p = playlists[i];

So is this really a good way of going through JSON?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:23:42+00:00Added an answer on May 23, 2026 at 10:23 am

    Most modern browsers support a native JSON.parse method that does exactly this. If you are targeting a browser that does not, you can use Douglas Crockford’s implementation provided here (scroll to the bottom).

    In any case, do not use eval if you can at all avoid it.

    Update (regarding eval):

    As JSON.org states:

    JSON is a subset of the object literal
    notation of JavaScript. Since JSON is
    a subset of JavaScript, it can be used
    in the language with no muss or fuss.

    What does this mean in practice? Well, it means that any valid JSON object is also valid JS code (in technical terms: it’s a valid JS expression that evaluates to an object literal). Therefore, you can paste JSON into a file and tell e.g. your browser to load it as JavaScript and it will do so without any error or warning (of course the “JavaScript” won’t actually do anything because it’s just the representation of an object and not procedural code).

    Since we established that JSON can be interpreted as JavaScript, it can also be passed to eval. Let’s see what the MDC has to say about eval:

    The argument of the eval function is a
    string. If the string represents an
    expression, eval evaluates the
    expression.

    …

    Parsing JSON (converting strings to JavaScript objects)

    If the string you’re calling eval() on contains data (for example, an array: "[1, 2, 3]"), as opposed to code, you should consider switching to JSON, which allows the string to use a subset of JavaScript syntax to represent data.

    The above is perhaps not the best explanation, but it does say what happens: JSON can be seen as a JS expression. eval takes a string representing a JS expression (in this case, a string containing JSON), evaluates it, and returns the result. It’s like a mini-compiler that you can feed code to at runtime, and it will run the code for you and return the result. In this case, the result will be a JS object (as already mentioned, JSON is a valid JS expression that represents an object).

    So if you have

    var json = '{"foo": "bar", "answer": 42}';
    var object1 = eval(json);
    
    var object2 = { foo: "bar", answer: 42 };
    

    Then object1 and object2 will be two different but identical objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking at learning some C since i saw in another SO question that is
I am looking for a quick way to share code snippets between developers. Visual
Yes another question that is a little Unique because I cant find the same
I'm looking for some information about the roll-out of a python project that uses
After looking at another question on SO ( Using NaN in C++ ) I
I'm currently primarily a D programmer and am looking to add another language to
I am looking for the best way to detect an image within another image.
I'm looking for a way to shutdown a windows app from another. Is there
I'm looking for a way to delete a file which is locked by another
What I am looking for is a way to call a method after another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.