user2852097
user2852097

Reputation: 53

Can't parse JSON Object, Stringify or get javascript property

I'm using a firefox addon that provides me with this variable:

[{errorMessage:"TypeError: a is null", sourceName:"http://pagead2.googlesyndication.com/pagead/js/graphics.js", 
lineNumber:17, console:null}]

From firebug, I can see this variable and its called "e".

I can type in e, and the print it as it is above.

If I type e.toString(); I get,

[object Object]

If I type e.errorMessage, it is undefined.

If I type JSON.parse(e), I get unexpected character error.

How can I get the information out of this object? It seems that anything I do to it, it just returns either [object Object] or undefined.

I've tried JSON.parse, JSON.stringify, iterating through it, and nothing provides me with the actual object information.

Upvotes: 2

Views: 4265

Answers (3)

Vitim.us
Vitim.us

Reputation: 22148

From the JSON Specification: [RFC 4627][1]

2.2. Objects

An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). A name is a string. A single colon comes after each name, separating the name from the value. A single comma separates a value from a following name. The names within an object SHOULD be unique.

2.5. Strings

A string begins and ends with quotation marks.


In the official ECMAScript specification:

It defines an Object Literal, which a PropertyNameAndValue can be a StringLiteral or an IdentifierLiteral. And an IdentifierLiteral, does not have quotes.

Unquoted keys names are legal and allowed in Javascript but they are not valid JSON.


http://jsonlint.com

[
    {
        errorMessage: "TypeError: a is null",
        sourceName: "http://pagead2.googlesyndication.com/pagead/js/graphics.js",
        lineNumber: 17,
        console: null
    }
]

Results

Parse error on line 3:
...a is null",        sourceName: "http://
----------------------^
Expecting 'STRING'

Upvotes: 0

Michael Geary
Michael Geary

Reputation: 28870

This is not JSON. It is a JavaScript array. It has nothing to do with JSON in any way.

To access a JavaScript array, just use ordinary JavaScript code, not JSON.parse or anything like that.

You could use JSON.stringify() to turn this array into JSON, but that certainly isn't what you want.

The reason e.toString() prints [object Object] is simply that this is what the .toString() method returns for an object or array. .toString() does not always give a useful result.

Paste the following into Firebug or the Chrome console and see what it logs:

var e = [
    {
        errorMessage: "TypeError: a is null",
        sourceName: "http://pagead2.googlesyndication.com/pagead/js/graphics.js",
        lineNumber: 17,
        console:null
    }
];

console.log( e.length );
console.log( e[0] );
console.log( e[0].errorMessage );
console.log( e[0].sourceName );
console.log( e[0].lineNumber );

Upvotes: 0

tymeJV
tymeJV

Reputation: 104785

That's an array containing an object, try this:

e[0].errorMessage;

Upvotes: 2

Related Questions