Not able to parse XML to JSON --Getting error as Unexpected Token:o

Ankur Khare
Tera Contributor

HI Team,

 

Below is sample code which is written in After Business Rule. I believe the issue is from line no 35 to line no 38 is not printing anything. I believe XML to JSON conversion is the issue.

In my try catch block I am getting error as "Unexpected Token o".

 

Attached is the sample code in notepad .If you see the catch block I am getting error as Unexpected Token o


         AnkurKhare_0-1722271967707.png

 

1 ACCEPTED SOLUTION

Hi @Ankur Khare 

please make the below changes:

 

 

// Parse JSON string back to an object
    var objHandle = JSON.parse(jsonString);
    gs.log('objHandle: ' + JSON.stringify(objHandle)); // Log objHandle for debugging
    
    // Access and log specific properties
    if (objHandle.net) {
        gs.log('parentNetHandle: ' + objHandle.net.parentNetHandle);
    } else {
        gs.log('The "net" property does not exist in the JSON object.');
    }

 

 

 

 

…………………………………………........................................................................................
Mark it helpful 👍and Accept Solution !! If this helps you to understand.

…………………………………………........................................................................................

View solution in original post

5 REPLIES 5

KevinBellardine
Kilo Sage

There's likely some character coming through in the XML that's tanking the JSON Parser. Print out the XML and look at it, you may need to replace some of the characters.

Satishkumar B
Giga Sage
Giga Sage

Hi @Ankur Khare 
The error message Unexpected Token o typically occurs when JSON.parse is called on an object instead of a JSON string. 
Can you try replacing code from line 35 with the below code:

 

var jsonObj = gs.xmlToJSON(responseBody);
gs.log('JSON Object: ' + JSON.stringify(jsonObj));
var jsonString = JSON.stringify(jsonObj);
// Parse JSON String into a JSON object
var objHandle = JSON.parse(jsonString);
gs.log('Parent Net Handle: ' + objHandle.parentNetHandle);

 


……………………………………………………………………………………………………

Please Mark it helpful 👍and Accept Solution✔️!! If this helps you!!

@Satishkumar B After making the above change, 

 var jsonObj = gs.xmlToJSON(responseBody); // Converting XML into a JSON object
            gs.log('jsonObj:' + jsonObj); --> I am getting  jsonObj:[object Object]
 
 var jsonString = JSON.stringify(jsonObj);
            gs.log('jsonString:' + jsonString); --> Getting jsonString:{"net":{"xmlns":"http://www.arin.net/regrws/core/v1","pocLinks":"","xmlns:ns2":"http://www.arin.net/regrws/messages/v1","netBlocks":{"netBlock":{"startAddress":"064.201.128.000","description":"Direct Allocation","type":"DA","cidrLength":"20","endAddress":"064.201.143.255"}},"netName":"TELWEST-BLK2","parentNetHandle":"NET-64-0-0-0-0","orgHandle":"UTC-68","registrationDate":"2008-04-01T18:18:49-04:00","handle":"NET-64-201-128-0-1","version":"4","originASes":""}}
 
 var objHandle = JSON.parse(jsonString);
            gs.log('objHandle:' + objHandle); -->Getting objHandle:[object Object]
 
gs.log('objHandle:1' + objHandle.parentNetHandle); -->Getting objHandle:1undefined
 
Let me what to change?

Hi @Ankur Khare 

please make the below changes:

 

 

// Parse JSON string back to an object
    var objHandle = JSON.parse(jsonString);
    gs.log('objHandle: ' + JSON.stringify(objHandle)); // Log objHandle for debugging
    
    // Access and log specific properties
    if (objHandle.net) {
        gs.log('parentNetHandle: ' + objHandle.net.parentNetHandle);
    } else {
        gs.log('The "net" property does not exist in the JSON object.');
    }

 

 

 

 

…………………………………………........................................................................................
Mark it helpful 👍and Accept Solution !! If this helps you to understand.

…………………………………………........................................................................................