JSON : Background Script Error

Vamsi26
Tera Contributor

Hello Everyone,

 

I'm trying to print the lastName from the system Property using Background Script:

 

System Property

 

{"firstName":"Ashutosh", "lastName":"Munot"}

 

 

Background Script:

 

var set = gs.getProperty('firstname_lastname');
var parsJSON = JSON.parse(set);
var parsedData = parsJSON.lastName;
gs.print(parsedData);

 

 

I'm trying to print the lastName from the system Property which is giving me error below Can someone help me with this

 

Evaluator: com.glide.script.RhinoEcmaError: Expected end of stream at char 240

 

Thanks

 

 
4 REPLIES 4

SN_Learn
Kilo Patron
Kilo Patron

Hi @Vamsi26 ,

 

I have tried this and it is working fine:

 

var set = gs.getProperty('firstname_lastname');

var parsJSON = JSON.parse(set);
var parsedData = parsJSON.lastName;
gs.print('Last Name: ' + parsedData);

 

SN_Learn_0-1731429059631.png

SN_Learn_1-1731429078200.png

 

 

Please check if there are any syntax/format error in your code. Please share screenshots to evaluate further if the issue still persists.

 

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Juhi Poddar
Kilo Patron

Hello @Vamsi26 

 

The script worked well in my PDI. This error occurs when there is some issue while parsing the JSON object. I would recommend to verify the value set in sys_properties table.

Properties configuration in sys_properties table:

JuhiPoddar_0-1731429213053.png

Note: Type: string, object should not be enclosed in double quote.

Script:

var set = gs.getProperty('firstname_lastname');
var parsJSON = JSON.parse(set);
var parsedData = parsJSON.lastName;
gs.print(parsedData);

Result:

JuhiPoddar_1-1731429329727.png

"If you found my answer helpful, please like and mark it as the "accepted solution". It helps others find the solution more easily and supports the community!"

Thank You
Juhi Poddar

 

 

Juhi Poddar
Kilo Patron

Hello @Vamsi26 

Thank you for marking my post helpful.

Can you please mark it as an accepted solution, as this will help future readers to find the solution easily and supports community.

 

Thank You 

Juhi Poddar 

Bhupender
Tera Expert

Hi @Vamsi26,

The error "Expected end of stream at char 240" suggests an issue with the JSON string in the system property.

Please verify that the system property "firstname_lastname" contains a valid JSON string by navigating to System Properties > All Properties. Ensure that it is correctly formatted; for instance:

```json
{"firstName":"Ashutosh", "lastName":"Munot"}
```

The script you wrote is functioning well, but the issue still persists.

I recommend updating the Background Script to include error handling. Here is a revised suggestion:

```javascript
try {
var set = gs.getProperty('firstname_lastname');
if (set) {
var parsJSON = JSON.parse(set);
gs.print(parsJSON.lastName);
} else {
gs.print('System property not found or is empty.');
}
} catch (e) {
gs.print('Error parsing JSON: ' + e.message);
}
```

I trust this will assist you in resolving the error.