ServiceNow REST API response column sort

Ann32
Kilo Contributor

Hello Everyone

      I am retrieving records from a table using REST API feature in servicenow. I selected columns in the query parameter (sysparm_fields) in a particular order . But while the API is being formed at the backend , the columns get re-ordered . How do i retain the native ordering of columns ? 

 

Thanks for the help in advance !

1 ACCEPTED SOLUTION

ben_knight
Kilo Guru

Please see this stack overflow response. ServiceNow has not adopted ES2015 yet so the relevant part is:

As a note, properties order in objects weren’t guaranteed at all before ES2015.

 

It is likely easier if you do not need to worry about the keys order, but if you must you can do something similar to what you need when iterating over the results though:

var keys = ["key_1", "key_2", "key_3"]

// run the query to retrieve the results. function not implemented here for simplicity.
// sysparm_fields would be set to keys.join();
var results = GetResults(keys);

results.forEach(function(record) {
  keys.forEach(function(k) {
    // K is now the first key you want
    var value = record[k];

    //... Do what you want here 
  });
});

View solution in original post

1 REPLY 1

ben_knight
Kilo Guru

Please see this stack overflow response. ServiceNow has not adopted ES2015 yet so the relevant part is:

As a note, properties order in objects weren’t guaranteed at all before ES2015.

 

It is likely easier if you do not need to worry about the keys order, but if you must you can do something similar to what you need when iterating over the results though:

var keys = ["key_1", "key_2", "key_3"]

// run the query to retrieve the results. function not implemented here for simplicity.
// sysparm_fields would be set to keys.join();
var results = GetResults(keys);

results.forEach(function(record) {
  keys.forEach(function(k) {
    // K is now the first key you want
    var value = record[k];

    //... Do what you want here 
  });
});