0

Can you tell me how to read the json in javascript?

I have a json string as bellow

 {"person":{"First name":"Dharmalingm","Last name":"Arumugam","Address":{"door number":"123","street":"sample street","city":"sample_city"},"phone number":{"mobile":"0123","landline":"01234","skype":"01235"}}}

I want to read the skype phone number

Hamish
23.4k8 gold badges56 silver badges67 bronze badges
asked May 23, 2012 at 21:07
3
  • 1
    Don't try to read the string. JSON IS javascript, it can be trivially converted into a native javascript object/array: json.org/js.html Commented May 23, 2012 at 21:08
  • 1
    Can you be a little more specific? Do you have the above JSON as a string value (i.e. "{"person":{...}}") or as an object (i.e. {"person":{...}})? Commented May 23, 2012 at 21:14
  • Is this the response to an AJAX call, or is it available when the initial page is rendered? Commented May 23, 2012 at 21:28

3 Answers 3

6

If you are starting out with a JSON string, start with 1. If you already have a JS object, then skip to 2.

  1. Parse the string using JSON.parse() to convert JSON string to a JS object. To support a browser that does not have the native JSON, you can use Crockford's JSON2 library to implement it.

    var jsondata = JSON.parse('{"person":{"First name":"Dharmalingm","Last name":"Arumugam","Address":{"door number":"123","street":"sample street","city":"sample_city"},"phone number":{"mobile":"0123","landline":"01234","skype":"01235"}}}');
    
  2. Retrieve the value like you normally would from a JS object

    var skype = jsondata.person['phone number'].skype;
    

Here's the full code and a sample:

var jsondata = JSON.parse('{"person":{"First name":"Dharmalingm","Last name":"Arumugam","Address":{"door number":"123","street":"sample street","city":"sample_city"},"phone number":{"mobile":"0123","landline":"01234","skype":"01235"}}}');
//normally, the dot-notation is used
//but since "phone number" is not a valid key when using dot-notation
//the bracket notation is used
var skype = jsondata.person['phone number'].skype;
answered May 23, 2012 at 21:09

Comments

5

This should get you on your way.

var o = {"person":{"First name":"Dharmalingm","Last name":"Arumugam","Address":{"door number":"123","street":"sample street","city":"sample_city"},"phone number":{"mobile":"0123","landline":"01234","skype":"01235"}}}
o["person"]["phone number"]["skype"];
/* or */
o.person["phone number"]["skype"];
answered May 23, 2012 at 21:10

1 Comment

If you don't have the "string" available in JavaScript as in this example (for instance if you get it from an external source) you should try @Joseph the Dreamer's answer.
0

First it needs to be parsed out to a native javascript object, in modern browsers this can be done via JSON.parse(json string here);. Now to specifically get to the skype number you have the object you just parsed out. Let's pretend you assigned it via var skypeData = JSON.parse(json string here);, the persons skype number is accessible via skypeData.person['phone number'].skype. We have to use ['phone number'] instead of person.phone number.skype because of the space.

answered May 23, 2012 at 21:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.