I have a service which returns XML resposne. I am using xml2JS to parse the XML to JSON. The XML looks like -
<root>
<children>
<type>custom</type>
<optional>false</optional>
<level>1</level>
<id>55705</id>
<title>Reference Resources</title>
<role>admin</role>
<children>
<mediano>1</mediano>
<id>be0_1436</id>
<title>Tests</title>
<level>2</level>
<optional>false</optional>
<role>admin</role>
<type>custom</type>
<children>
<mediano>2</mediano>
<id>be1_1401</id>
<title>Word Documents</title>
<level>3</level>
<optional>false</optional>
<role>admin</role>
<type>custom</type>
<children>
<mediano>3</mediano>
<id>be2_1389</id>
<title>Progress Tests Units 1-2A</title>
<level>4</level>
<optional>false</optional>
<role>admin</role>
<type>public</type>
</children>
<children>
<mediano>4</mediano>
<id>be3_1395</id>
<title>Progress Tests Units 1-2B</title>
<level>4</level>
<optional>false</optional>
<role>admin</role>
<type>public</type>
</children>
<children>
<mediano>5</mediano>
<id>be4_1387</id>
<title>Progress Tests Units 3-4A</title>
<level>4</level>
<optional>false</optional>
<role>admin</role>
<type>public</type>
</children>
<children>
<mediano>6</mediano>
<id>be5_1382</id>
<title>Progress Tests Units 3-4B</title>
<level>4</level>
<optional>false</optional>
<role>admin</role>
<type>public</type>
</children>
</children>
<children>
<mediano>7</mediano>
<id>be6_1374</id>
<title>Audios</title>
<level>3</level>
<optional>false</optional>
<role>admin</role>
<type>custom</type>
<children>
<mediano>8</mediano>
<id>be7_1354</id>
<title>Track 1</title>
<level>4</level>
<optional>false</optional>
<role>admin</role>
<type>public</type>
</children>
<children>
<mediano>9</mediano>
<id>be8_1346</id>
<title>Track 2</title>
<level>4</level>
<optional>false</optional>
<role>admin</role>
<type>public</type>
</children>
<children>
<mediano>10</mediano>
<id>be9_1328</id>
<title>Track 3</title>
<level>4</level>
<optional>false</optional>
<role>admin</role>
<type>public</type>
</children>
<children>
<mediano>11</mediano>
<id>be10_1326</id>
<title>Track 4</title>
<level>4</level>
<optional>false</optional>
<role>admin</role>
<type>public</type>
</children>
<children>
<mediano>12</mediano>
<id>be11_1315</id>
<title>Tests Audio Scripts</title>
<level>4</level>
<optional>false</optional>
<role>admin</role>
<type>public</type>
</children>
</children>
</children>
<children>
<mediano>13</mediano>
<id>be12_1305</id>
<title>Answer Key</title>
<level>2</level>
<optional>false</optional>
<role>admin</role>
<type>public</type>
</children>
<children>
<mediano>14</mediano>
<id>e0adb276a4c91bd4e040140ad4036164</id>
<title>Stopwatch</title>
<level>2</level>
<optional>false</optional>
<role>admin</role>
<type>public</type>
</children>
<children>
<mediano>15</mediano>
<id>e253b516eaa57b95e040140ad5035634</id>
<title>Scoreboard</title>
<level>2</level>
<optional>false</optional>
<role>admin</role>
<type>public</type>
</children>
</children>
<children>
<type>custom</type>
<optional>false</optional>
<level>1</level>
<id>55706</id>
<title>title shots</title>
<role>all</role>
<children>
<mediano>1</mediano>
<id>ecb70c0e55965669e040140ad5033e0b</id>
<title>title shots</title>
<level>2</level>
<optional>false</optional>
<role>all</role>
<type>public</type>
</children>
<children>
<mediano>2</mediano>
<id>191bd859902e7801e0532502140a370c</id>
<title>html url public</title>
<level>2</level>
<optional>false</optional>
<role>all</role>
<type>public</type>
</children>
</children>
<children>
<type>custom</type>
<optional>false</optional>
<level>1</level>
<id>55707</id>
<title>Media and Games</title>
<role>all</role>
<children>
<mediano>1</mediano>
<id>be13_1299</id>
<title>Podcasts and Worksheets</title>
<level>2</level>
<optional>false</optional>
<role>all</role>
<type>custom</type>
<children>
<mediano>2</mediano>
<id>be14_1290</id>
<title>Unit 1</title>
<level>3</level>
<optional>false</optional>
<role>all</role>
<type>custom</type>
<children>
<mediano>3</mediano>
<id>be15_1280</id>
<title>Podcast</title>
<level>4</level>
<optional>false</optional>
<role>all</role>
<type>public</type>
</children>
<children>
<mediano>4</mediano>
<id>be16_1272</id>
<title>Worksheet</title>
<level>4</level>
<optional>false</optional>
<role>all</role>
<type>public</type>
</children>
<children>
<mediano>5</mediano>
<id>be17_1266</id>
<title>Script</title>
<level>4</level>
<optional>false</optional>
<role>admin</role>
<type>public</type>
</children>
</children>
<children>
<mediano>6</mediano>
<id>be18_1250</id>
<title>Unit 2</title>
<level>3</level>
<optional>false</optional>
<role>all</role>
<type>custom</type>
<children>
<mediano>7</mediano>
<id>be19_1240</id>
<title>Podcast</title>
<level>4</level>
<optional>false</optional>
<role>all</role>
<type>public</type>
</children>
<children>
<mediano>8</mediano>
<id>be20_1231</id>
<title>Worksheet</title>
<level>4</level>
<optional>false</optional>
<role>all</role>
<type>public</type>
</children>
<children>
<mediano>9</mediano>
<id>be21_1216</id>
<title>Script</title>
<level>4</level>
<optional>false</optional>
<role>admin</role>
<type>public</type>
</children>
</children>
<children>
<mediano>10</mediano>
<id>be22_1189</id>
<title>Unit 3</title>
<level>3</level>
<optional>false</optional>
<role>all</role>
<type>custom</type>
<children>
<mediano>11</mediano>
<id>be23_1131</id>
<title>Podcast</title>
<level>4</level>
<optional>false</optional>
<role>all</role>
<type>public</type>
</children>
<children>
<mediano>12</mediano>
<id>be24_1052</id>
<title>Worksheet</title>
<level>4</level>
<optional>false</optional>
<role>all</role>
<type>public</type>
</children>
<children>
<mediano>13</mediano>
<id>be25_896</id>
<title>Script</title>
<level>4</level>
<optional>false</optional>
<role>admin</role>
<type>public</type>
</children>
</children>
</children>
<children>
<mediano>14</mediano>
<id>be26_801</id>
<title>Audio Bank</title>
<level>2</level>
<optional>false</optional>
<role>all</role>
<type>custom</type>
<children>
<mediano>15</mediano>
<id>be27_786</id>
<title>Unit 1</title>
<level>3</level>
<optional>false</optional>
<role>all</role>
<type>custom</type>
<children>
<mediano>16</mediano>
<id>be28_777</id>
<title>Recording 1</title>
<level>4</level>
<optional>false</optional>
<role>all</role>
<type>public</type>
</children>
<children>
<mediano>17</mediano>
<id>be29_769</id>
<title>Recording 2</title>
<level>4</level>
<optional>false</optional>
<role>all</role>
<type>public</type>
</children>
<children>
<mediano>18</mediano>
<id>be30_767</id>
<title>Recording 3</title>
<level>4</level>
<optional>false</optional>
<role>all</role>
<type>public</type>
</children>
<children>
<mediano>19</mediano>
<id>be31_756</id>
<title>Recording 4</title>
<level>4</level>
<optional>false</optional>
<role>all</role>
<type>public</type>
</children>
<children>
<mediano>20</mediano>
<id>be32_750</id>
<title>Recording 5</title>
<level>4</level>
<optional>false</optional>
<role>all</role>
<type>public</type>
</children>
<children>
<mediano>21</mediano>
<id>be33_746</id>
<title>Recording 6</title>
<level>4</level>
<optional>false</optional>
<role>all</role>
<type>public</type>
</children>
<children>
<mediano>22</mediano>
<id>be34_740</id>
<title>Recording 7</title>
<level>4</level>
<optional>false</optional>
<role>all</role>
<type>public</type>
</children>
</children>
</children>
<children>
<mediano>23</mediano>
<id>be35_731</id>
<title>Games</title>
<level>2</level>
<optional>false</optional>
<role>all</role>
<type>custom</type>
<children>
<mediano>24</mediano>
<id>be36_720</id>
<title>Pelmanism</title>
<level>3</level>
<optional>false</optional>
<role>all</role>
<type>public</type>
</children>
<children>
<mediano>25</mediano>
<id>be37_718</id>
<title>Hangman</title>
<level>3</level>
<optional>false</optional>
<role>all</role>
<type>public</type>
</children>
<children>
<mediano>26</mediano>
<id>be38_712</id>
<title>Doorbuster</title>
<level>3</level>
<optional>false</optional>
<role>all</role>
<type>public</type>
</children>
</children>
<children>
<mediano>27</mediano>
<id>13bef4738dd56268e0532402140ab184</id>
<title>Smart secret Player</title>
<level>2</level>
<optional>false</optional>
<role>all</role>
<type>custom</type>
<children>
<mediano>28</mediano>
<id>13bef4738dd66268e0532402140ab184</id>
<title>pss_1</title>
<level>3</level>
<optional>false</optional>
<role>all</role>
<type>public</type>
</children>
<children>
<mediano>29</mediano>
<id>13bef4738dd86268e0532402140ab184</id>
<title>pss_2</title>
<level>3</level>
<optional>false</optional>
<role>all</role>
<type>public</type>
</children>
</children>
</children>
</children>
</root>
After parsing the JSON looks like -
[
{
"type": "custom",
"optional": "false",
"level": "1",
"id": "55705",
"title": "Reference Resources",
"role": "admin",
"children": [
{
"mediano": "1",
"id": "be0_1436",
"title": "Tests",
"level": "2",
"optional": "false",
"role": "admin",
"type": "custom",
"children": [
{
"mediano": "2",
"id": "be1_1401",
"title": "Word Documents",
"level": "3",
"optional": "false",
"role": "admin",
"type": "custom",
"children": [
{
"mediano": "3",
"id": "be2_1389",
"title": "Progress Tests Units 1-2A",
"level": "4",
"optional": "false",
"role": "admin",
"type": "public"
},
{
"mediano": "4",
"id": "be3_1395",
"title": "Progress Tests Units 1-2B",
"level": "4",
"optional": "false",
"role": "admin",
"type": "public"
},
{
"mediano": "5",
"id": "be4_1387",
"title": "Progress Tests Units 3-4A",
"level": "4",
"optional": "false",
"role": "admin",
"type": "public"
},
{
"mediano": "6",
"id": "be5_1382",
"title": "Progress Tests Units 3-4B",
"level": "4",
"optional": "false",
"role": "admin",
"type": "public"
}
]
},
{
"mediano": "7",
"id": "be6_1374",
"title": "Audios",
"level": "3",
"optional": "false",
"role": "admin",
"type": "custom",
"children": [
{
"mediano": "8",
"id": "be7_1354",
"title": "Track 1",
"level": "4",
"optional": "false",
"role": "admin",
"type": "public"
},
{
"mediano": "9",
"id": "be8_1346",
"title": "Track 2",
"level": "4",
"optional": "false",
"role": "admin",
"type": "public"
},
{
"mediano": "10",
"id": "be9_1328",
"title": "Track 3",
"level": "4",
"optional": "false",
"role": "admin",
"type": "public"
},
{
"mediano": "11",
"id": "be10_1326",
"title": "Track 4",
"level": "4",
"optional": "false",
"role": "admin",
"type": "public"
},
{
"mediano": "12",
"id": "be11_1315",
"title": "Tests Audio Scripts",
"level": "4",
"optional": "false",
"role": "admin",
"type": "public"
}
]
}
]
},
{
"mediano": "13",
"id": "be12_1305",
"title": "Answer Key",
"level": "2",
"optional": "false",
"role": "admin",
"type": "public"
},
{
"mediano": "14",
"id": "e0adb276a4c91bd4e040140ad4036164",
"title": "Stopwatch",
"level": "2",
"optional": "false",
"role": "admin",
"type": "public"
},
{
"mediano": "15",
"id": "e253b516eaa57b95e040140ad5035634",
"title": "Scoreboard",
"level": "2",
"optional": "false",
"role": "admin",
"type": "public"
}
]
},
{
"type": "custom",
"optional": "false",
"level": "1",
"id": "55706",
"title": "title shots",
"role": "all",
"children": [
{
"mediano": "1",
"id": "ecb70c0e55965669e040140ad5033e0b",
"title": "title shots",
"level": "2",
"optional": "false",
"role": "all",
"type": "public"
},
{
"mediano": "2",
"id": "191bd859902e7801e0532502140a370c",
"title": "html url public",
"level": "2",
"optional": "false",
"role": "all",
"type": "public"
}
]
},
{
"type": "custom",
"optional": "false",
"level": "1",
"id": "55707",
"title": "Media and Games",
"role": "all",
"children": [
{
"mediano": "1",
"id": "be13_1299",
"title": "Podcasts and Worksheets",
"level": "2",
"optional": "false",
"role": "all",
"type": "custom",
"children": [
{
"mediano": "2",
"id": "be14_1290",
"title": "Unit 1",
"level": "3",
"optional": "false",
"role": "all",
"type": "custom",
"children": [
{
"mediano": "3",
"id": "be15_1280",
"title": "Podcast",
"level": "4",
"optional": "false",
"role": "all",
"type": "public"
},
{
"mediano": "4",
"id": "be16_1272",
"title": "Worksheet",
"level": "4",
"optional": "false",
"role": "all",
"type": "public"
},
{
"mediano": "5",
"id": "be17_1266",
"title": "Script",
"level": "4",
"optional": "false",
"role": "admin",
"type": "public"
}
]
},
{
"mediano": "6",
"id": "be18_1250",
"title": "Unit 2",
"level": "3",
"optional": "false",
"role": "all",
"type": "custom",
"children": [
{
"mediano": "7",
"id": "be19_1240",
"title": "Podcast",
"level": "4",
"optional": "false",
"role": "all",
"type": "public"
},
{
"mediano": "8",
"id": "be20_1231",
"title": "Worksheet",
"level": "4",
"optional": "false",
"role": "all",
"type": "public"
},
{
"mediano": "9",
"id": "be21_1216",
"title": "Script",
"level": "4",
"optional": "false",
"role": "admin",
"type": "public"
}
]
},
{
"mediano": "10",
"id": "be22_1189",
"title": "Unit 3",
"level": "3",
"optional": "false",
"role": "all",
"type": "custom",
"children": [
{
"mediano": "11",
"id": "be23_1131",
"title": "Podcast",
"level": "4",
"optional": "false",
"role": "all",
"type": "public"
},
{
"mediano": "12",
"id": "be24_1052",
"title": "Worksheet",
"level": "4",
"optional": "false",
"role": "all",
"type": "public"
},
{
"mediano": "13",
"id": "be25_896",
"title": "Script",
"level": "4",
"optional": "false",
"role": "admin",
"type": "public"
}
]
}
]
},
{
"mediano": "14",
"id": "be26_801",
"title": "Audio Bank",
"level": "2",
"optional": "false",
"role": "all",
"type": "custom",
"children": {
"mediano": "15",
"id": "be27_786",
"title": "Unit 1",
"level": "3",
"optional": "false",
"role": "all",
"type": "custom",
"children": [
{
"mediano": "16",
"id": "be28_777",
"title": "Recording 1",
"level": "4",
"optional": "false",
"role": "all",
"type": "public"
},
{
"mediano": "17",
"id": "be29_769",
"title": "Recording 2",
"level": "4",
"optional": "false",
"role": "all",
"type": "public"
},
{
"mediano": "18",
"id": "be30_767",
"title": "Recording 3",
"level": "4",
"optional": "false",
"role": "all",
"type": "public"
},
{
"mediano": "19",
"id": "be31_756",
"title": "Recording 4",
"level": "4",
"optional": "false",
"role": "all",
"type": "public"
},
{
"mediano": "20",
"id": "be32_750",
"title": "Recording 5",
"level": "4",
"optional": "false",
"role": "all",
"type": "public"
},
{
"mediano": "21",
"id": "be33_746",
"title": "Recording 6",
"level": "4",
"optional": "false",
"role": "all",
"type": "public"
},
{
"mediano": "22",
"id": "be34_740",
"title": "Recording 7",
"level": "4",
"optional": "false",
"role": "all",
"type": "public"
}
]
}
},
{
"mediano": "23",
"id": "be35_731",
"title": "Games",
"level": "2",
"optional": "false",
"role": "all",
"type": "custom",
"children": [
{
"mediano": "24",
"id": "be36_720",
"title": "Pelmanism",
"level": "3",
"optional": "false",
"role": "all",
"type": "public"
},
{
"mediano": "25",
"id": "be37_718",
"title": "Hangman",
"level": "3",
"optional": "false",
"role": "all",
"type": "public"
},
{
"mediano": "26",
"id": "be38_712",
"title": "Doorbuster",
"level": "3",
"optional": "false",
"role": "all",
"type": "public"
}
]
},
{
"mediano": "27",
"id": "13bef4738dd56268e0532402140ab184",
"title": "Smart secret Player",
"level": "2",
"optional": "false",
"role": "all",
"type": "custom",
"children": [
{
"mediano": "28",
"id": "13bef4738dd66268e0532402140ab184",
"title": "pss_1",
"level": "3",
"optional": "false",
"role": "all",
"type": "public"
},
{
"mediano": "29",
"id": "13bef4738dd86268e0532402140ab184",
"title": "pss_2",
"level": "3",
"optional": "false",
"role": "all",
"type": "public"
}
]
}
]
}
]
The key 'children' can have one record or multiple records. When the key 'children' has one record, the parser makes the type as object and when there are multiple records, it becomes an array, which as I understand is the way in which xml2JS parser works. Per my requirement, I want this key to be an array irrespective of the number of records it has. Also, this is an array, which can be nested to n-th level and the key 'children' should be an array of objects, always. Search for "children": {. I want this to be "children": [
This is a sample response. There can be multiple similar instances. Can anyone help with this issue?