Sidebar

error when trying to add a new array instance in json object

0 votes
281 views
asked Jan 11, 2022 by nickie-l-1757 (200 points)

Can you help with the following error:  

[line: 27] WrappedException: Wrapped com.qvera.qie.web.exception.MessageModelException: Unexpected character ('l' (code 108)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: java.io.StringReader@4f5a235; line: 2, column: 20] 

Stack Trace:
  at script [line: 27]

 

-----------------------------------------------------------------------------------------------------------------------------------

surgeons/[1]/firstName - contains a string representing an array of surgeon names to be parse

//LAST1, FIRST1; LAST2, FIRST2 MIDDLE2; LAST3, FIRST3 MIDDLE3;

In the above there are 3 "surgeon" that need to go back into the message as

surgeons/[1]/firstName = FIRST1

surgeons/[1]/lastName = LAST1

surgeons/[2]/firstName = FIRST2

surgeons/[2]/lastName = LAST2

surgeons/[3]/firstName = FIRST3

surgeons/[3]/lastName = LAST3

when I attempt to add the 2nd array instance back to surgeons I get the above error

 

-------------------------------------------------------------------------------------------------------------------------------------------------
var surgeons = StringUtils.splitByWholeSeparatorPreserveAllTokens(message.getNode('surgeons/[1]/firstName'), ';');
qie.info('surgeons: ' + message.getNode('surgeons/[1]/firstName'));
qie.info('length: ' + surgeons.length);
for (var i = 0; i < surgeons.length; i++) {
   //split the value into separate strings
   qie.info('surgeons[' + i + '] ' + surgeons[i]);
   var lastName = StringUtils.substringBefore(surgeons[i], ",");
   var firstName = StringUtils.substringAfter(surgeons[i], ",");
   qie.info("lastname: " + lastName);
   qie.info("firstname: " + firstName);
   qie.info("index: " + i);
   var j = i + 1;
   qie.info("j: " + j);

   //message.setNode('surgeons/[' + j + ']/firstName',firstName,j,true);
   //message.setNode('surgeons/[' + j + ']/lastName',lastName,j,true);
   if (j == 1) {
      qie.info("testing j=1");
      message.setNode('surgeons/[1]/firstName',firstName);
      message.setNode('surgeons/[1]/lastName',lastName);
      qie.info("testing j=1");
   }   
   if (j >= 2) {
      qie.info("testing j>1");
      // create new surgeon object
      var surgeon = qie.parseJSONString('{\n' +
         '      "lastName": lastName,\n' +
         '      "firstName": firstName,\n' +

         '      "npi:" + ""\n' +
         '   }');

      // add to array
      message.addObjectToJSONArray('/surgeons', surgeon);
   }
}

 

1 Answer

0 votes

To give you an example of how to extract an array from your JSON object and then maybe add a new field like fullName you could use the following example:

var surgeons = message.getAllNodes('surgeons/[]'); // save off an array of surgeons
qie.debug('surgeons: ' + message.getNode('surgeons/[1]/firstName'));
qie.debug('length: ' + surgeons.length);
for (var i = 0; i < surgeons.length; i++) {
   //split the value into separate strings
   qie.debug('surgeons[' + i + '] ' + surgeons[i]);
   var surgeonInstance = qie.parseJSONString(surgeons[i], 'UTF-8'); // parse this instance of our array as JSON so that we can extract the data elements we require
   var lastName = surgeonInstance.getNode("lastName");
   var firstName = surgeonInstance.getNode("firstName");
   qie.debug("lastname: " + lastName);
   qie.debug("firstname: " + firstName);
   qie.debug("index: " + i);
   var j = i + 1;
   qie.debug("j: " + j);
   
   message.setNode('surgeons/['+j+']/fullName', firstName + ' ' + lastName); // combo the first and last names into one new field of fullName
}
 

answered Jan 11, 2022 by michael-h-5027 (14,390 points)
...