1

im new to json. Ive created objects similar in structure to the attached json string. I believe what ive created, intentionally, is a nested array object...

 {
 "tracelog":{
 "title":"",
 "phase":"12",
 "users":"",
 "logduration":"",
 "logdurationtype":"undefined",
 "showlogduedate":"no",
 "durationstartfield":"",
 "fields":[
 {
 "atts":[
 {
 "name":"dfvdfv",
 "fieldtype":"text",
 "followedbydate":"no",
 "datetype":"---",
 "fieldduration":"---",
 "fielddurationtype":"undefined",
 "fielddurationstart":"",
 "autocalcunits":"none",
 "validationtype":"none",
 "fieldvisibleto":"",
 "fieldaccessibleto":"",
 "dropdown":""
 }
 ]
 },
 {
 "atts":[
 {
 "name":"ffff",
 "fieldtype":"text",
 "followedbydate":"no",
 "datetype":"---",
 "fieldduration":"---",
 "fielddurationtype":"undefined",
 "fielddurationstart":"",
 "autocalcunits":"none",
 "validationtype":"none",
 "fieldvisibleto":"",
 "fieldaccessibleto":"",
 "dropdown":""
 }
 ]
 }
 ]
 }
}

What i want to do is find the length of the array named "atts". Ive parsed the JSON string as follows...

var obj = jQuery.parseJSON(jsonstring);

... and later call the objects thusly ...

obj.tracelog.fields.length

accurately tells me how many fields there are.

obj.tracelog.fields[0].atts.name

... gives me the name, but throws a console error saying field[0] is undefined. so it works, and it doesnt. while ....

obj.tracelog.fields[0].atts.length

does NOT give me the number of fields in the "atts" array (if it is infact an array - id like it to be) but gives me "undefined".

Can someone point out where im going wrong here?

thanks.

Elliot B.
17.7k11 gold badges85 silver badges103 bronze badges
asked May 22, 2014 at 2:02
3
  • It doesn't look like you are doing anything wrong. Commented May 22, 2014 at 2:06
  • Looking at your object structure, obj.tracelog.fields[0].atts is actually an array. So this could not have worked: obj.tracelog.fields[0].atts.name. It wouldn't have errored out, but it should have returned undefined, not the name. You would have needed to use: obj.tracelog.fields[0].atts[0].name Commented May 22, 2014 at 2:23
  • You don't need jQuery for this. JSON.parse is available in ES5 compliant hosts, there is a polyfill for those that aren't. atts is an array of length 1, it contains a single member that is an object. Perhaps you want the count of properties on that object? e.g. Object.keys(obj.tracelog.fields[0].atts[0]).length in ES5 compliant hosts. Commented May 22, 2014 at 3:02

1 Answer 1

2

obj.tracelog.fields[0].atts is an array so you must access it like so:

obj.tracelog.fields[0].atts[0].name

Working example

Addressing the latter part of your question:

obj.tracelog.fields[0].atts.length does give you the number of items in the array, which in your example is one object. It seems what you actually want to do is count the number of properties on the first object in that array (i.e., obj.tracelog.fields[0].atts[0]). There is no simple .length style property that will give you the number of properties on an object, but you can loop over them and count like this:

var count = 0;
for (var test in obj.tracelog.fields[0].atts[0]) {
 count++;
}
 alert(count);
answered May 22, 2014 at 2:22

6 Comments

Object.keys(your_object).length is also a nice one liner, even if less efficient.
Thanks Elliot. Yes, that returns the name, however console still returns "TypeError: obj.tracelog.fields[0] is undefined" - but then the next line returns the name per your advice. AND, "obj.tracelog.fields[0].atts.length returns "1" as opposed to "12" - shouldnt it be 12?
Oh, or should each of the elements be in brackets? i will test.
I updated my answer to elaborate probably at the same time you were writing that comment. obj.tracelog.fields[0].atts.length should return 1 because it is an array with one object in it. What you want to do is count the number of properties on obj.tracelog.fields[0].atts[0] object which I've provided in my example above.
@Jerska Yeah, that is a really nice one-liner, but it won't work on older browsers.
|

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.