1

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);

                        },

2 Answers 2

2

In your code, the following example shows how to access the properties in this particular JSON object:

alert( obj.COLUMNS[0] );  // alerts the string "NAME".

alert( obj.DATA[0][0] );   // alerts "YOUNG, MARIA              "

To understand why this is the output, it's important to understand and learn to read the notation that makes up JSON:

{} = object

[] = array

Your JSON:

{
    "COLUMNS": ["NAME"],
    "DATA":  [["YOUNG, MARIA              "]]
}

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:

var your_object = new Object();
your_object.name = "Bob";
your_object.skills = ["programmer","debugger","writer"];

console.info( JSON.stringify( your_object ) );

// this would convert the object to JSON. Going the other direction could help 
  // you further understand the concepts under the hood. Here is the output:
{ "name" : "Bob", "skills" : [ "programmer" , "debugger" , "writer" ] }
Sign up to request clarification or add additional context in comments.

Comments

0

once you've run your object through parseJSON you'll have an object that has two children

myObj = parseJSON (yourJSONStringAbove);
myObj.COLUMNS ; // this is an array containing one item whose value is "name"
myObj.DATA; // this is an array of arrays, it contains one array which has one value

so, myObj.data[0][0] ; // this is "YOUNG, MARIA          "

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.