0

I have an ajax call that returns the following JSON:

returnedData = "[ 
    { id: 1, firstName: 'John', lastName: 'Smith', address: '123 Spa Road', city: 'London', 
        orders: 
        [ 
            { product: 'TV', price: 599.99, quantity: 2, orderTotal: 1199.98 } 
        ]
    }
]";

var customers = JSON.parse (returnedData);
console.log(customers.length); // prints length of the string data

It treats it as string. However, it I assign the result directly.

var customers = [ 
    { id: 1, firstName: 'John', lastName: 'Smith', address: '123 Spa Road', city: 'London', 
        orders: 
        [ 
            { product: 'TV', price: 599.99, quantity: 2, orderTotal: 1199.98 } 
        ]
    }
];

console.log(customers.length); // prints 1 - the number of objects

Why is it like this? How can I assign it dynamically?

7
  • 1
    Not sure what you mean with prints length as string Commented Dec 19, 2013 at 14:10
  • 1
    The way that code is written, it will not return anything but an error. "Uncaught SyntaxError: Unexpected token ILLEGAL" Commented Dec 19, 2013 at 14:14
  • 4
    That's not valid JSON. It would be a valid Javascript literal, but it's not JSON. Commented Dec 19, 2013 at 14:14
  • 2
    The code isn't even valid JavaScript since you cannot have multiline strings in JS. Commented Dec 19, 2013 at 14:15
  • 1
    [{"id":1,"firstName":"John","lastName":"Smith","address":"123 Spa Road","city":"London","orders":[{"product":"TV","price":599.99,"quantity":2,"orderTotal":1199.98}]}] is valid JSON data. Commented Dec 19, 2013 at 14:16

5 Answers 5

3

Run the code through http://jsonlint.com/ and you will find out you have errors in your code.

Pasting your object literal into the validator you will see

Parse error on line 2:
[    {        id: 1,        first
--------------^
Expecting 'STRING', '}'

You need to have double quotes around the name and string values.

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

Comments

1

you have problems with you string...

here is your well formed json

var returnedData ='[
    {
        "id": 1,
        "firstName": "John",
        "lastName": "Smith",
        "address": "123SpaRoad",
        "city": "London",
        "orders": [
            {
                "product": "TV",
                "price": 599.99,
                "quantity": 2,
                "orderTotal": 1199.98
            }
        ]
    }
]';

single quotes must be escaped or don't use them... you can use them when you open and close the string so its more well formed than other posibilitys (correted because of useful comment from @Victor Canova)

2 Comments

you can always check your json code here: http://jsonlint.com/
Just curious, "single quotes arent allowed" you meant that they must be scaped if used inside JSON, right?
1

You have to wrap the attributes with double quotes:

var returnedData = '[{ "id": 1, "firstName": "John"}]';

Comments

1

The string you're receiving isn't a valid JSON object. Be careful not to confuse JSON and JavaScript. Look at this example:

var returnedData = '[ { "id": 1, "firstName": "John", "lastName": "Smith", "address": "123 Spa Road", "city": "London", "orders": [ { "product": "TV", "price": 599.99, "quantity": 2, "orderTotal": 1199.98 } ] } ]';

var customers = JSON.parse (returnedData);
console.log(customers.length); // the correct length now.

Note that the only difference is that I put the keys in the dictionary within "'s.

1 Comment

Thank you. That is what I was missing.
1

returnedData isn't a valid JSON message (attributes are not wrapped with double quotes), but you can treat it as JS doing

var customers = eval(returnedData);

If you want to use JSON.parse, correct the returnedData.

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.