4

ive got a problem getting parameters from a json chain, the json I got looks something like this

[{"aa":"bb","ccc":"ddd","eeee":"ffff","ggggg":"hhhhh","iiiiii":"jjjjjj","kkkkkkk":"lllllll"}]

Im trying to count how many pairs there are inside the '{}' but i dont know how. I tried json.length and json[0].length, the first one gave me back the value '1' and the second one undefined.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836

1 Answers1

2

Your json object is an array containing one object. So the length is 1. The object in the array has multiple properties (key/value pairs).

So in most modern browsers (except IE), this would work for you:

Object.keys(json[0]).length

Check out the answers here of various techniques for iterating/counting properties of an object in JavaScript:

Community
  • 1
  • 1
kaliatech
  • 17,579
  • 5
  • 72
  • 84