JSON : Background Script Error
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2024 08:00 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2024 08:33 AM
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);
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2024 08:42 AM
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:
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:
"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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2024 06:42 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2024 09:24 PM
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.