Accessing json content in javascript function

If I have JSON response like the following:

{
	"my_document_list": [{

			"doc1": "445",
			"doc2": "445",
			"doc3": "445",
			"doc4": "445"

		}

	]
}

So I am accessing the JSON something like this in my javascript code inside my function :

this.processDocument = function(data_,textStatus_,jqXHR_) {

            var collection = data_.my_document_list; // This gives access to all the values from `doc1 to doc 4`

However, if I have the JSON response like the following :

{
	"my_document_list": [{

			"doc1": "445",
			"doc2": "445",
			"doc3": "445",
			"doc4": "445",
			"content": "Some text here to display"
		}

	]
}

And if I have to select only the content part which is inside my_document_list, is it a right way to do ?:

this.processContent = function(data_,textStatus_,jqXHR_) {

            var collection = data_.content;

When I do something like this : var collection = data_.my_document_list.data_.content;

I get an error saying “Uncaught TypeError: Cannot read property content of undefined”

… then you have invalid JSON.

Thanks for pointing that out. I have updated the valid json. I didn’t write valid json here by mistake before.

data_.my_document_list is an array, and it has no data_ property. Did you mean something like

var content = data_.my_document_list[0].content

… or if you need the content of more than the first document, you might do something like

function getContent (documentItem) {
  return documentItem.content
}

var contents = data_.my_document_list.map(getContent)

data_.my_document_list[0].content sounded reasonable, but when I tried this, I am getting an error : Uncaught TypeError: Cannot read property '0' of undefined

Assuming data_ is an object like the one you gave as a sample response, it works fine for me…

Thanks. Surprisingly same thing doesn’t works in my code :disappointed:

Do you know how to use a debugger? You might set a break point at the content assignment, and inspect the values of the function parameters to see why data_.my_document_list is undefined.

Thanks. I will try to look at debugger thing. Haven’t used it before. I was checking by putting console.log(data_) inside my function and I saw the data printing. For some reason it’s failing at this data_.my_document_list[0].content line only.

When I ran these two lines inside my function:

this.processContent = function(data_,textStatus_,jqXHR_) {

            
			console.log("Checking for Data:"+data_); 
			var collection = data_.my_document_list[0].content;

I get Checking for Data:[object Object]

and

Uncaught TypeError: Cannot use 'in' operator to search for '1222' in Result Type: jquery-1.11.1.js:583

After researching here, I found out that this in operator works only on objects. And as far as my console log is concerned above, I am seeing object Object. Hence wondering what could be the cause of this error?

That’s impossible to tell from that snippet alone, since there isn’t even an in operator used. The error is happening somewhere else in your code; it looks like you somewhere called a jQuery function with invalid arguments. You should see a call stack in the console to see where you made that call.

BTW, you’ll get a full representation of the object if you don’t coerce it to a string by concatenating it with another string. Try this instead:

console.log("Checking for Data:", data_)

Thanks for mentioning about not coerceing thing. Actually, my JSON response also includes webservice status which I didn’t include. So, this is my complete JSON Response which may help in understanding what I am trying to ask bel:

{
	"webservice_status": {
		"status": "SUCCESS",
		"message": ""
	},


	"my_document_list": [{

			"doc1": "445",
			"doc2": "445",
			"doc3": "445",
			"doc4": "445",
			"content": "Some text here to display"
		}

	]
}

When I did console.log("Checking for Data:", data_) in my function, I noticed the following in the log:

Checking for Data: Object {webservice_status:Object,my_document_list:Array[1]}

Do you think instead of my_document_list:Array[1] showing as an array, it should be an Object in order for that to work?
Probably this could be the reason the in operator from wherever it is coming(which I am figuring out) is not working.
But I am thinking, can my_document_list:Array[1] be represented as an my_document_list: Object ?

For what to work? ^^ We’d really need the bit of code that causes the error. data_.my_document_list[0] returns the first element of that array, which is an object – so no problem so far.

No, arrays are also objects, so the in operator works on them as well. Maybe some other part of your program expects an object with a specific structure or something, but again, that’s impossible to tell without actually seeing it.

If that array always contains only one single element, then the array wouldn’t be necessary indeed.

Thanks again. Sorry for not providing more details. Here is a new topic I created since it was getting very big.

Thanks for your time. Appreciated. I was able to fix the bug by modifying this line var collection = data_.my_document_list.length[0].content; to var collection = data_.my_document_list.length; I believe it wasn’t jQuery related issue which I was thinking before.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.