JSON And Object
Use JSON and object utilities when a script needs to parse third-party responses, build request bodies, or map external fields into Soln field API names.
Common Methods
| Method | Description |
|---|---|
JSON.stringify(value) | Converts an object to JSON text. Use this when an API signature needs the raw body text |
JSON.parse(text) | Parses JSON text into an object. Use it only when the response is text and must be parsed manually |
Object.entries(object) | Converts an object into [key, value] pairs for filtering or mapping |
Object.fromEntries(entries) | Converts [key, value] pairs back into an object. Useful after removing empty values |
record[key] | Reads a dynamic field from an object. Check the value type before using it |
typeof value | Checks runtime type, such as string, number, or boolean |
Array.isArray(value) | Checks whether a response field is an array before iterating it |
Number(value) | Converts external numeric text into a number before writing it into a numeric field |
| `String(value |
Build A JSON Request Body
const body = {
orderNo: 'SO-1001',
amount: 1200,
status: 'paid',
};
const response = await context.http.requestJson({
url: 'https://example.com/api/orders',
method: 'POST',
json: body,
});
Parse A Text Response As JSON
Use this when an API returns JSON text but cannot be handled by requestJson.
const response = await context.http.request({
url: 'https://example.com/api/orders',
expect: 'text',
});
const data = JSON.parse(response.bodyText) as {
orders: Array<{ id: string; amount: number }>;
};
Read Object Fields Safely
function readText(record: Record<string, unknown>, key: string) {
const value = record[key];
return typeof value === 'string' ? value : '';
}
const orderNo = readText(externalOrder, 'order_no');
Remove Empty Values
function omitEmptyValues(input: Record<string, unknown>) {
return Object.fromEntries(
Object.entries(input).filter(([, value]) => (
value !== undefined &&
value !== null &&
value !== ''
))
);
}
const data = omitEmptyValues({
orderNo: externalOrder.order_no,
amount: externalOrder.amount,
remark: externalOrder.remark,
});
Map External Fields To Soln Fields
const data = {
orderNo: externalOrder.order_no,
amount: Number(externalOrder.amount || 0),
customerName: externalOrder.customer_name,
};
await context.form.create({
formApiName: 'orders',
title: String(data.orderNo || ''),
data,
});
Notes
Use json in context.http when the request body is a plain object. Use JSON.parse only when the script has a text response that must be parsed manually. Validate the response shape before writing data into Soln records.