parseJSON
codebolt.outputparsers.parseJSON(jsonString: string): Object
Parses JSON string and returns a result object with success flag and parsed data or error.
Parameters
Name | Type | Description |
---|---|---|
jsonString | string | The JSON string to parse. |
Response Structure
The method returns an object with the following structure:
Success Response
{
success: true,
parsed: <parsed_json_object>
}
Error Response
{
success: false,
error: <Error_object>
}
Examples
Valid JSON Parsing
const validJson = '{"name": "test", "value": 123, "active": true}';
const result = await codebolt.outputparsers.parseJSON(validJson);
console.log(result);
// Output: { success: true, parsed: { name: 'test', value: 123, active: true } }
Invalid JSON Parsing
const invalidJson = '{"name": "test", "value": 123, "active":}';
const result = await codebolt.outputparsers.parseJSON(invalidJson);
console.log(result);
// Output: { success: false, error: SyntaxError... }
Complex JSON Object
const complexJson = JSON.stringify({
users: [
{ id: 1, name: 'Alice', roles: ['admin', 'user'] },
{ id: 2, name: 'Bob', roles: ['user'] }
],
metadata: {
total: 2,
timestamp: new Date().toISOString()
}
});
const result = await codebolt.outputparsers.parseJSON(complexJson);
console.log(result);
// Output: { success: true, parsed: { users: [...], metadata: {...} } }