1

The following two lines of javascript code cause the Uncaught SyntaxError:

    var testObj = JSON.parse('{ testFunc: function () { console.log("whoa there"); }}');

    testObj["testFunc"]();

I don't understand what is invalid about my json string.

1
  • 2
    jsonlint.org will tell you. What you have is simply not JSON. Commented Jul 27, 2012 at 16:54

2 Answers 2

5

As far as I am aware, that is not a JSON string, but rather javascript held by a string literal. JSON is, just like XML, a means to represent data across multiple platforms. Please have a look at the specification for JSON:

http://www.json.org

If you want to pass values that are actually functions, you need to use the javascript eval method alternatively let JSON string values work as keys to an object that maintains these methods.

Sign up to request clarification or add additional context in comments.

Comments

3

JSON doesn't allow for inline function definitions... that's JavaScript.

http://json.org/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.