Tuesday 16 May 2017

Weird json data from xml2js

I'm trying to parse XML to JSON in Nodejs, but I'm getting back some very weird JSON data.

This is the XML data:

<?xml version="1.0" encoding="UTF-8" ?>
<RESPONSE>
<SINGLE>
<KEY name="users"><MULTIPLE>
<SINGLE>
<KEY name="id"><VALUE>1</VALUE>
</KEY>
<KEY name="username"><VALUE>guest</VALUE>
</KEY>
<KEY name="firstname"><VALUE>Guest user</VALUE>
</KEY>
<KEY name="lastname"><VALUE> </VALUE>
</KEY>
<KEY name="fullname"><VALUE>Guest user  </VALUE>
</KEY>
<KEY name="email"><VALUE>root@localhost</VALUE>
</KEY>
<KEY name="address"><VALUE null="null"/>
</KEY>
<KEY name="phone1"><VALUE null="null"/>
</KEY>
<KEY name="phone2"><VALUE null="null"/>
</KEY>
<KEY name="icq"><VALUE null="null"/>
</KEY>
<KEY name="skype"><VALUE null="null"/>
</KEY>
<KEY name="yahoo"><VALUE null="null"/>
</KEY>
<KEY name="aim"><VALUE null="null"/>
</KEY>
<KEY name="msn"><VALUE null="null"/>
</KEY>
<KEY name="department"><VALUE></VALUE>
</KEY>
<KEY name="institution"><VALUE null="null"/>
</KEY>
<KEY name="idnumber"><VALUE null="null"/>
</KEY>

The last value, 'idnumber', is the value I have to retrieve from all the users.

This is the generated json:

{ RESPONSE:
   { SINGLE:
      [ { KEY:
           [ { '$': { name: 'users' },
               MULTIPLE:
                [ { SINGLE:
                     [ { KEY:
                          [ { '$': { name: 'id' }, VALUE: [ '1' ] },
                            { '$': { name: 'username' }, VALUE: [ 'guest' ] },
                            { '$': { name: 'firstname' }, VALUE: [ 'Guest user' ] },
                            { '$': { name: 'lastname' }, VALUE: [ ' ' ] },
                            { '$': { name: 'fullname' }, VALUE: [ 'Guest user  ' ] },
                            { '$': { name: 'email' }, VALUE: [ 'root@localhost' ] },
                            { '$': { name: 'address' },
                              VALUE: [ { '$': { null: 'null' } } ] },
                            { '$': { name: 'phone1' },
                              VALUE: [ { '$': { null: 'null' } } ] },
                            { '$': { name: 'phone2' },
                              VALUE: [ { '$': { null: 'null' } } ] },
                            { '$': { name: 'icq' }, VALUE: [ { '$': { null: 'null' } } ] },
                            { '$': { name: 'skype' }, VALUE: [ { '$': { null: 'null' } } ] },
                            { '$': { name: 'yahoo' }, VALUE: [ { '$': { null: 'null' } } ] },
                            { '$': { name: 'aim' }, VALUE: [ { '$': { null: 'null' } } ] },
                            { '$': { name: 'msn' }, VALUE: [ { '$': { null: 'null' } } ] },
                            { '$': { name: 'department' }, VALUE: [ '' ] },
                            { '$': { name: 'institution' },
                              VALUE: [ { '$': { null: 'null' } } ] },
                            { '$': { name: 'idnumber' },
                              VALUE: [ { '$': { null: 'null' } } ] },

Can anyone help me set up a loop to retrieve only the 'idnumbers'? Or a way to clean up the json, because this looks kind of wrong...

Thanks in advance!!



via Milan_w

No comments:

Post a Comment