101

I am returning a json as shown below

{"name": "", "skills": "", "jobtitel": "Entwickler", "res_linkedin": "GwebSearch"}

I am trying to get each element key and value:

..
}).done(function(data){
    alert(data['jobtitel']);
});

I am getting undefined in alert. WHY? I tried data.jobtitel, i tried loop but no success..

5
  • first u need to convert that json to array then have to try data.jobtitel Commented Sep 20, 2013 at 7:15
  • JSON is string and u want to access there that then u must have to convert... Commented Sep 20, 2013 at 7:18
  • @swan, $.parseJSON is not always available in every browser.. what to do ? Commented Sep 20, 2013 at 7:19
  • 2
    if u add jquery then $.parseJSON will work...otherwise u have to use pure javascript JSON converter JSON.eval(jsonstring) Commented Sep 20, 2013 at 7:22
  • Is jobtitel a typo? Commented Jun 2, 2015 at 13:11

8 Answers 8

169
//By using jquery json parser    
var obj = $.parseJSON('{"name": "", "skills": "", "jobtitel": "Entwickler", "res_linkedin": "GwebSearch"}');
alert(obj['jobtitel']);

//By using javasript json parser
var t = JSON.parse('{"name": "", "skills": "", "jobtitel": "Entwickler", "res_linkedin": "GwebSearch"}');
alert(t['jobtitel'])

Check this jsfiddle

As of jQuery 3.0, $.parseJSON is deprecated. To parse JSON strings use the native JSON.parse method instead.

Source: http://api.jquery.com/jquery.parsejson/

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

2 Comments

This is not documented enough. The ability to use json['KeyAsString']. Really great. Thank you.
How would I resolve Entwickler to jobtitel? Is there anythind native or do I have to write my own code?
24

you have parse that Json string using JSON.parse()

..
}).done(function(data){
    obj = JSON.parse(data);
    alert(obj.jobtitel);
});

1 Comment

Simplified: JSON.parse(data).jobtitle
13
var data = {"name": "", "skills": "", "jobtitel": "Entwickler", "res_linkedin": "GwebSearch"}

var parsedData = JSON.parse(data);
alert(parsedData.name);
alert(parsedData.skills);
alert(parsedData.jobtitel);
alert(parsedData.res_linkedin);

1 Comment

This is how you do it in javascript.
6

For getting key

var a = {"a":"1","b":"2"};
var keys = []
for(var k in a){
  keys.push(k)
}

For getting value.

var a = {"a":"1","b":"2"};
var values = []
for(var k in a){
  values.push(a[k]);
}

Comments

4

http://jsfiddle.net/v8aWF/

Worked out a fiddle. Do check it out

(function() {
    var oJson = {
        "name": "", 
        "skills": "", 
        "jobtitle": "Entwickler", 
        "res_linkedin": "GwebSearch"
    }
    alert(oJson.jobtitle);
})();

Comments

3

A simple approach instead of using JSON.parse

 success: function(response){
     var resdata = response;
     alert(resdata['name']);
}

Comments

3

It looks like data not contains what you think it contains - check it.

let data={"name": "", "skills": "", "jobtitel": "Entwickler", "res_linkedin": "GwebSearch"};

console.log( data["jobtitel"] );
console.log( data.jobtitel );

Comments

0

You can use the following solution to get a JSON key and value in JavaScript:

var dt = JSON.stringify(data).replace('[', '').replace(']', '');
if (dt) {
  var result = jQuery.parseJSON(dt);
  var val = result.YOUR_OBJECT_NAME;
}

1 Comment

While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

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.