1

I have JSON data such as this:

var data = '{"credit":{"@code":"NT2C8FFC","card":"NT2C8FFC","credit":"149.96","amount":"149.96","disabled":"0","expired":"2011-02-15 10:21:18","user":"xxxx","issued":"2010-02-15 10:21:18","orderid":[],"fromorder":[],"email":"xxxx","phone":[],"state":[],"address":[],"city":[],"zip":[],"country":[],"customerid":"xxx","order":"xxx","order_date":"2010-01-23 00:00:00","reason":"Product Not Working as Expected","source":"xxx","first":[],"last":[],"notes":[]}}'

It is actually being returned to me as JSON. I am just displaying it here like this for example sake. I can use data["credit"] to get the element called credit but that only gets me :

{"@code":"NT2C8FFC","card":"NT2C8FFC","credit":"149.96","amount":"149.96","disabled":"0","expired":"2011-02-15 10:21:18","user":"xxxx","issued":"2010-02-15 10:21:18","orderid":[],"fromorder":[],"email":"xxxx","phone":[],"state":[],"address":[],"city":[],"zip":[],"country":[],"customerid":"xxx","order":"xxx","order_date":"2010-01-23 00:00:00","reason":"Product Not Working as Expected","source":"xxx","first":[],"last":[],"notes":[]}

How can I drill down to one shot? data["credit"]["credit"] is not proper syntax. It results in undefined. I know I'm close but can't remember the rest. The goal is to get 149.96 from the JSON data.

6
  • 2
    Why do you say it's not proper syntax? Commented Apr 10, 2013 at 6:44
  • @ExplosionPills 's fiddle works fine for me.. Commented Apr 10, 2013 at 6:57
  • @ExplosionPills, your Fiddle does not include the quotes around the data which is why it works Commented Apr 10, 2013 at 7:15
  • possible duplicate of I have a nested data structure / JSON, how can I access a specific value? Commented Apr 10, 2013 at 15:51
  • Could it be that you somehow double encoded your data and data.credit is a actually a string containing JSON? If so, fix your data! Commented Apr 10, 2013 at 15:54

3 Answers 3

1

For now you can use alert(data.credit.credit).

        var data = '{"credit":{"@code":"NT2C8FFC","card":"NT2C8FFC","credit":"149.96","amount":"149.96","disabled":"0","expired":"2011-02-15 10:21:18","user":"xxxx","issued":"2010-02-15 10:21:18","orderid":[],"fromorder":[],"email":"xxxx","phone":[],"state":[],"address":[],"city":[],"zip":[],"country":[],"customerid":"xxx","order":"xxx","order_date":"2010-01-23 00:00:00","reason":"Product Not Working as Expected","source":"xxx","first":[],"last":[],"notes":[]}}';
        eval("data=" + data);
        alert(data.credit.credit);

You can also use data = JSON.parse(data); to convert the string into Json but this has browser compatibility issues.

These are the browsers that supports

  • Firefox (Mozilla) 3.5
  • Internet Explorer 8
  • Chrome
  • Opera 10
  • List item
  • Safari 4

Older versions dont support.

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

7 Comments

. or [] syntax will both work correctly for an object. Using [] syntax allows a variable to be used between the brackets, i.e. var c='credit';console.log(data[c]);
The example of "data" in the context is data being returned as a JSON object. That was why I used data["credit"]. I just can't get the inner credit.
"data" is a JSON object. My bad for putting quotes around it. data.credit.credit returns undefined.
"You can also use data = JSON.parse(data); to convert the string into Json": No no no! You use JSON.parse to convert JSON into a native object / array. If you wanted to create JSON, you would take an existing object/array and pass it to JSON.stringify. There is no such thing as a JSON object.
@FelixKling: And what about the browser compatibility issues? Say I want this on IE7..will JSON.parse work?
|
0

use . operator to get the object's object

alert(data.credit.credit) //gives 149.96
 alert(data.credit.amount) //gives 149.96
alert(data.credit.card) //gives NT2C8FFC

Comments

0

You are making data a string not an object. Change

var data = '{"credit":{"@code":"NT2C8FFC","card":"NT2C8FFC","credit":"149.96","amount":"149.96","disabled":"0","expired":"2011-02-15 10:21:18","user":"xxxx","issued":"2010-02-15 10:21:18","orderid":[],"fromorder":[],"email":"xxxx","phone":[],"state":[],"address":[],"city":[],"zip":[],"country":[],"customerid":"xxx","order":"xxx","order_date":"2010-01-23 00:00:00","reason":"Product Not Working as Expected","source":"xxx","first":[],"last":[],"notes":[]}}'

to

var data = {"credit":{"@code":"NT2C8FFC","card":"NT2C8FFC","credit":"149.96","amount":"149.96","disabled":"0","expired":"2011-02-15 10:21:18","user":"xxxx","issued":"2010-02-15 10:21:18","orderid":[],"fromorder":[],"email":"xxxx","phone":[],"state":[],"address":[],"city":[],"zip":[],"country":[],"customerid":"xxx","order":"xxx","order_date":"2010-01-23 00:00:00","reason":"Product Not Working as Expected","source":"xxx","first":[],"last":[],"notes":[]}}

If you can't change the original data you will need to parse it by doing

data = JSON.parse(data);

(which will work in most browsers) or use a method from a library such as JQuery's:

data = jQuery.parseJSON(data);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.