1

While parsing a JSON object, is there any better way to convert all single length objects to array, because when I want to run a loop using length it works well for arrays but fail if length is 1 and it is an object (not included in []).

module:{section:{topic:"some topic"}, section:{[{topic:"some topic1"},{topic:"some topic2"},{topic:"some topic3"}]}} 

My question is module.section.length is good for second section but undefined for first section. So is there any way to convert all objects inside JSON into arrays?

Surreal Dreams
  • 26,055
  • 3
  • 46
  • 61
shikhar chauhan
  • 431
  • 1
  • 4
  • 9
  • Can you please show us how you would want the data represented after converting it to your preferred format? – Surreal Dreams Jun 19 '15 at 02:08
  • Yes there is, [this has been already asked before](http://stackoverflow.com/questions/20881213/converting-json-object-into-javascript-array). – Spencer Wieczorek Jun 19 '15 at 02:09
  • Better than what? Also, this is not JSON and the overall problem has nothing to do with JSON. – Felix Kling Jun 19 '15 at 02:11
  • 2
    The data you listed isn't JSON. It isn't a valid JavaScript object or array either. It's in a completely novel format. Is this what the data really looks like? Or can you list the actual JSON string you're receiving? – Michael Geary Jun 19 '15 at 02:13
  • may be it is my bad that, I did not ask question clearly... forget about the task i want to achieve i just want to know is there any easy way to convert {module:{section:{topic:"some topic"}}, section:{[{topic:"some topic1"},{topic:"some topic2"},{topic:"some topic3"}]}} } INTO {module:{section:[{topic:"some topic"}]}, section:{[{topic:"some topic1"},{topic:"some topic2"},{topic:"some topic3"}]}} } .... so that all "topic" are coming within [] even there is single topic in section. I am actually using x2js and it is giving this type of data – shikhar chauhan Jun 19 '15 at 02:23
  • Can you please update your question accordingly? So people are all aware of any updates or missing information you added later. – TaoPR Jun 19 '15 at 03:27

0 Answers0