How to use JSON in workflow script?

ayordanov
Kilo Contributor

Hello,

I have the following issue. I have a table with a field called "json_output". The user must filled it with valid json and then I want to process this data in a workflow script. So far, I've read about

var parser = new JSONParser();

var blades = parser.parse(current.json_output.toString());

But it seems it doesn't work in server-side scripts. When the script is executed it outputs the following message:

JSONParser undefined, maybe missing global qualifier

Can anybody tell me how to use JSON in workflow script?

Thanks,

Alexander

1 ACCEPTED SOLUTION

You can also use JSON method to decode your json_output. See below,



var json = new global.JSON();


var blades = new JSON().decode(current.json_output.toString());



gs.log('keyName1 - ' + blades.keyName1);



View solution in original post

5 REPLIES 5

guhann
Mega Guru

Just add a global keyword to access the JSONParser.



var parser = new global.JSONParser();  


var blades = parser.parse(current.json_output.toString());


ayordanov
Kilo Contributor

That solved the UI error message, but led to another. According to ServiceNow system logs, the variable parser is undefined - Caught exception in InterpretedScript <refname>: org.mozilla.javascript.JavaScriptException: org.mozilla.javascript.WrappedException: WrappedException of The undefined value has no properties.



If i write the gs.addInfoMessage(new global.JSONParser()); seems everything is ok because it prints [Object], but if i try to call anything from that, it gives me error in the system logs.


I attempted this from both background script and business rule, and it worked okay for me. To do a little testing, can you try something like the following?



var JSONObj = '{"name":"Alexander","lastname":"Yordanov"}';


var parser = new JSONParser();


var blades = parser.parse(JSONObj);


gs.print(blades.name);


//Should get 'Alexander'



This should be a simple proof of concept, and will help in determining what the issue is.


Hi Alexander,



The script include JSONParser is not client-callable, i.e. you can only use it on the server side...   A few questions:



  1. You mentioned a UI error message, where are you trying to use it?
  2. Also, what version SNOW are you using?
  3. And maybe copy in your latest script(s) that are kicking back an error.


Thanks,


-Brian