This will probably be easy, but I need to access the values in this JSON object. I have no idea how. Here’s the object:
{
"COLUMNS": ["NAME"],
"DATA": [["YOUNG, MARIA "]]
}
I was hoping “obj.NAME” would do it, but it says it’s undefined. Here’s my AJAX call:
$.ajax({
//this is the php file that processes the data and send mail
url: 'components/Person.cfc',
//GET method is used
type: "POST",
//pass the data
data: {
method: "getGroup",
uid: $('#cardText').val()
},
success: function(response) {
var obj = $.trim(response);
var obj = jQuery.parseJSON(obj);
$('#form_result').html(obj.NAME);
},
In your code, the following example shows how to access the properties in this particular JSON object:
To understand why this is the output, it’s important to understand and learn to read the notation that makes up JSON:
Your JSON:
Since the outermost part is represented by curly braces, we know the JSON represents an object, not an array. The object has two properties, both of which are arrays, since the values assigned to them are wrapped in brackets.
The second property, DATA, is actually an array of size 1 that contains another array of size 1 that contains a string.
Finally, in your code, you’re trying to access NAME, which is a value, not a property. JSON the final point to understand about JSON is that all objects are represented by key/value pairs. You use the key to access the value. obj.COLUMNS retrieves the first array, and obj.DATA retrieves the second array.
NAME is not a property. Instead, it is a value assigned to the array.
To help you learn how to access the JSON, practice accessing the properties of different objects. Additionally, you can convert existing objects back to JSON and display them in your console so that you can see how they are structured in JSON: