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 3947598
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:16:15+00:00 2026-05-20T01:16:15+00:00

I had a json parsing problem updating my app from prototype 1.6.1 to 1.7.0

  • 0

I had a json parsing problem updating my app from prototype 1.6.1 to 1.7.0
This is a very simplified model of my json as it, saved in tmp.js:

{
"text":"hello world",
"fn": function(){alert('hello world')
}

and this is my code:

new Ajax.Request('tmp.js', {
onSuccess: function(transport){
    var json = transport.responseText.evalJSON();

    var button = new Element('button')
            .update(json.text)
        .observe('click', function(){
            json.fn();
        });
    $('my_div').update(button);
}});

All this worked correctly with 1.6.1: it produced a button that alerted ‘hello world’ on click.
This does not work in v. 1.7.0, because of the fact that my json is not valid. I know it should not contain functions, but only data.

My question is: why did it worked with 1.6.1 (and still works) and is there a way to accomplish the same with 1.7.0. I need to get via ajax a js object containing user defined functions.

Thank you

Update:
The reconstruct function is a good solution and I think I’ll use it in the future.
Anyway I found eval() function that seems to be a good and fast solution:

tp.js JSON:

{
"text":"hello world",
"fn": "my_alert('hello world')"
}

JS

function my_alert(string){
    alert(string);
}

new Ajax.Request('tmp.js', {
onSuccess: function(transport){
    var json = transport.responseText.evalJSON();

    var button = new Element('button')
            .update(json.text)
        .observe('click', function(){
            eval(json.fn);
        });
    $('my_div').update(button);
}});
  • 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-20T01:16:16+00:00Added an answer on May 20, 2026 at 1:16 am

    What you’ve got in that sample data you posted is not JSON. In strict JSON the value of a property can be

    • a string
    • a number
    • boolean true or false
    • null
    • an array
    • an object

    There is no way to include a function definition in JSON. (Well, that’s not exactly true; you’re free to use strings, numbers, arrays, objects, etc. to describe a function in such a way that your code can reconstruct it after the JSON is parsed. The point is that straight JavaScript function expressions are disallowed.)

    One simple, slightly disturbing thing you could do is save the function body as a string, and then reconstruct it by calling

    foo.fn = new Function(foo.fn);
    

    once the JSON parse is complete.

    edit more details:

    The “Function()” constructor takes as its arguments a list of strings representing argument names, followed by a string that’s to be used as the function body. If you wanted to encode a complete JavaScript function, therefore, you might want to have it look like an object:

    {
      'foo': 'plain property',
      'someFunction': {
        'arguments': [ 'x', 'y' ],
        'body': 'if (x > y) return "x"; return "y";'
      }
    }
    

    Now to turn “someFunction” into a real function, you’d use something like this:

    function reconstructFunction(descr) {
      var params = (descr.arguments || []).slice(0);
      params.push(descr.body);
      return Function.apply(null, params);
    }
    

    Then you can just pass the function descriptor from your JSON into something like that, and then you have a bona fide JavaScript function to call.

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

Sidebar

Related Questions

Had this working; at one stage. The problem is the following text is now
I was wondering if anyone had any pointers for parsing json data consumed from
I had this problem before and can't for life of me remember how to
I’m having trouble getting a AJAX/JSON function to work correctly. I had this function
I’m having trouble getting a AJAX/JSON function to work correctly. I had this function
Part of a website's JSON response had this (... added for context): {..., now:function(){return(new
I had been parsing my JSON quite nicely but my server just changed on
A] Problem summary: I have JSON data being returned from python to javascript. I
I had developed an app in android that take some data from a database
I've having issues with parsing JSON data from a php file through to JQuery.I've

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.