Import Set REST API - Transform Map - Can I use a JSON Array in API request body?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2024 05:23 PM - edited ‎03-18-2024 05:25 PM
Hi,
I am working with Import Set REST API in ServiceNow. I am trying to import an array of data to use for post-processing in an onAfter script. However, the array is not accessible within the script. I can access anything else other than what is enclosed in the [ ] bracket.
Specifically using the "Insert Multiple Records from the same request" API.
Request body looks like this:
{
"records": [
{
"u_field_1": "String", // Accessible
"u_field_2": "String", // Accessible
"u_field_3": "String", // Accessible
"u_field_4": [ // Inaccessible
{ // Inaccessible
"subfield_1": "String", // Inaccessible
"subfield_2": "String" // Inaccessible
}, // Inaccessible
{ // Inaccessible
"subfield_1": "String", // Inaccessible
"subfield_2": "String" // Inaccessible
} // Inaccessible
] // Inaccessible
}
]
}
Any help around how to access this array within the scripts would be highly appreciated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2024 05:49 PM
Hi @usmankaramat, Can you share the script you have to access the JSON object? As u_field_4 is an array, you can try to loop through the length to access the values inside u_field_4.
var data = '{"records": [{"u_field_1": "String","u_field_2": "String","u_field_3": "String","u_field_4": [{"subfield_1": "String", "subfield_2": "String"},{"subfield_1": "String","subfield_2": "String"}]}]}'
var parsedData = JSON.parse(data);
var recordsArray = parsedData.records;
for (var i = 0; i < recordsArray.length; i++) {
var field4Array = recordsArray[i].u_field_4;
for (var j = 0; j < field4Array.length; j++) {
var subfield1Value = field4Array[j].subfield_1;
var subfield2Value = field4Array[j].subfield_2;
}
}
Regards,
Sunil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2024 06:02 PM - edited ‎03-18-2024 06:24 PM
Hi @SunilKumar_P ,
I have set up the script as being somewhat similar to yourself. The issue lies within getting the data (or the String) to come into the script itself. Just to clarify, the field which I had referenced in the code above is "u_network_adapter". I have also set up a log to see what value the field holds. Any value other than the array is accessible, such as a string, boolean etc.
I have set-up parsing as well as you have described. But that throws an error, as it encounters an empty string which is coming in.
As far as the loop is concerned, it never really gets to that point, as it throws an error on the lines above.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2024 06:14 PM
Hi @usmankaramat, as you are parsing the network adapters, does the network adapters field contains the complete JSON object? Also if the JSON object contains the array in it then you can try using another for loop.
Regards,
Sunil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2024 06:19 PM - edited ‎03-19-2024 06:20 PM
@SunilKumar_P That is coming in as empty too.