The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

TechNow Ep 37 | Understanding JSON

Chuck Tomasi
ServiceNow Employee
ServiceNow Employee

expert-logo-2.png

Constructing easy integrations to other systems is a big part of what makes ServiceNow such a wonderful application development platform. Sometimes the challenge lies in figuring out exactly what you have been given and how to apply it in ServiceNow. In this episode, the TechNow crew will guide you through one of the most common data exchange formats - JSON. Even though the results from your remote system are text, it may be difficult to understand it and get access to the specific property or value in JavaScript. As always, the team starts with some easy examples to help you understand the fundamentals and build more complex scenarios to help you interpret and access complex data objects. Community quick tip: Avoiding pitfalls with platform objects.

Originally aired April 13, 2017 12:00PM PDT  
(Be sure to change your YouTube Setting to 720HD)

Ask your questions below on this discussion page.

And Please Let our Experts know how they've helped! Comment Below!

Like, Share, Mark Helpful.


Featured Experts

find_real_file.pngChuck Tomasi is a Platform Architect for ServiceNow.   He is a computer science major with over 30 years of IT experience. As a former ServiceNow customer, Chuck won the first Innovation of the Year Award at Knowledge 10. Since joining ServiceNow in 2010 as a Technical Consultant, he has done many large scale ITSM implementations and custom applications, acted as an adjunct instructor for Education Services, created and lead the Technical Best Practices program, and co-hosts the ServiceNow series "TechNow".

 

find_real_file.pngDave Slusher has been developing software for 20 years for companies such as Intel, Orbitz, Dell Secureworks and many startups lost to history. He has been with ServiceNow for two years, first in Expert Services and now as the Developer Evangelist for the developer community and portal. He earned his BS from Georgia Tech and his MS in Computer Science from the University of Louisiana - Lafayette.

 

find_real_file.pngKreg Steppe is a Senior Curriculum Developer within ServiceNow developing and supporting cloud training infrastructure. He specializes in developing integration solutions, automating repeatable processes and Cloud Management in ITOM. Kreg's prior experience includes operating his own ISP, developing web applications in PHP, network integration, managing network support, Application Development on cloud based networks, DNS and email server maintenance. He is a Linux enthusiast and enjoys Photography.


2 REPLIES 2

Chuck Tomasi
ServiceNow Employee
ServiceNow Employee

Here is the code and config for TechNow ep 37.



Quick Tip Repository



Demo 1:


// Simple JavaScript object with property


var person = { "name": "Chuck"};


gs.info(person.name);



Demo 2:


// JavaScript Object with 2 properties



var person = {


      "name": "Chuck",


      "age" : 52


};


gs.info(person.name + ' is ' + person.age);



Demo 3:


// Simple stringify example


var person = {


      "name": "Chuck",


      "age" : 52


};



person.children   = [];


var child1 = {"name" : "Julie", "age" : 23};


var child2 = {"name" : "Liisa", "age" : 21};


person.children.push(child1);


person.children.push(child2);



gs.info(JSON.stringify(person));



Demo 4:


// Accessing Objects


var personString = '{ "name": "Chuck", "age" : 52, "children" : [ {"name" : "Julie", "age" : 23}, {"name" : "Liisa", "age" : 21} ] }';


var person = JSON.parse(personString);



gs.info('person.name=' + person.name);



if (person.children) {


  for (var i = 0; i < person.children.length; i++) {


      gs.info('Child ' + i + '=' + person.children[i].name);


  }


}



Demo 5:


// Using a response back from an integration


try {


var r = new sn_ws.RESTMessageV2();


r.setHttpMethod('GET');


r.setEndpoint('https://democt1.service-now.com/api/x_66238_weather/forecast/2017-04-13');



var response = r.execute();


var responseBody = response.getBody();


gs.info('responseBody=' + responseBody);


gs.info('Hi Temp=' + responseBody.result.temp.hi);


var httpStatus = response.getStatusCode();


} catch(ex) {


var message = ex.getMessage();


}



Demo 6:


// Making complex objects more readible


// using JSON.stringify(obj, replacer, space)



try {


var r = new sn_ws.RESTMessageV2();


r.setHttpMethod('GET');


r.setEndpoint('https://en.wikipedia.org/api/rest_v1/feed/onthisday/events/04/13');



var response = r.execute();


var responseBody = response.getBody();


gs.info('responseBody=' + responseBody);



// var doc = JSON.parse(responseBody);


// gs.info(JSON.stringify(doc, null, 4));



var httpStatus = response.getStatusCode();


} catch(ex) {


var message = ex.getMessage();


}


Lisa Latour
Administrator
Administrator

Did you participate?


We'd love to hear your feedback on this event!


Please Take the Survey - and tell us what you think!



oh and


Find More Events on the Community!