Import Set REST API - Transform Map - Can I use a JSON Array in API request body?

usmankaramat
Tera Contributor

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. 

 

4 REPLIES 4

SunilKumar_P
Giga Sage

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

usmankaramat
Tera Contributor

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. 

usmankaramat_0-1710809994893.png

 

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

usmankaramat
Tera Contributor

@SunilKumar_P That is coming in as empty too.