cancel
Showing results for 
Search instead for 
Did you mean: 

How to parse JSON in javascript

SOLVED
Occasional Contributor

How to parse JSON in javascript

Hello, community.

 

I couldn't find a way to parse JSON in javascript. Could anybody help me?

I casual JS I can use JSON.parse. But it is invalid case for SOAPUI.

 

I know  that for grove it's a lot of examples with JsonSlurper, in my case I'm using only javascript.

 

My scenario:

I have REST request which returns we JSON.

To this request I have added Script Assertion in which I want to check value from JSON response and value from custom property.

 

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Moderator

Re: How to parse JSON in javascript

SoapUI 5.3 uses an older version of the Rhino engine (1.7R2) that does not support JSON.parse(), but there are a couple of workarounds.

 

Option 1: use eval()

var str = '{"foo": "bar"}';
var obj = eval('(' + str + ')');
log.info(obj.foo);

 

Option 2: Update Rhino engine and use JSON.parse()

  1. Download the latest version of Rhino from here:
    https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Download_Rhino
  2. In the <SoapUI>\lib folder, replace js-1.7R2.jar with js-<ver>.jar from the downloaded archive.
  3. Restart SoapUI.

Now you can use JSON.parse():

var str = '{"foo": "bar"}';
var obj = JSON.parse(str);
log.info(obj.foo);

Helen Kosova
SmartBear Technical Writer
________________________

 Join SmartBear Connect next April in Boston to meet with software development, API and testing experts from around the world! Get your ticket today!

View solution in original post

6 REPLIES 6
Moderator

Re: How to parse JSON in javascript

Hi divergentor,

 

Have you considered using JSON assertions instead?

 

json-assertion.png


Helen Kosova
SmartBear Technical Writer
________________________

 Join SmartBear Connect next April in Boston to meet with software development, API and testing experts from around the world! Get your ticket today!
Occasional Contributor

Re: How to parse JSON in javascript

Hello, Helen.

 

I thought about it as a first solution.

But I need customer property to be incremented.

I couldn't find any option to increment value from JSONpath.

That is why i decided that Script Assertion will be the best solution.

 

For now I did it by adding one additional Script step, in which I take customer property, increment it and write back.
Then I take it and compare as you show.

 

But I want to use javascript because it can simplify a lot of things.

Moderator

Re: How to parse JSON in javascript

SoapUI 5.3 uses an older version of the Rhino engine (1.7R2) that does not support JSON.parse(), but there are a couple of workarounds.

 

Option 1: use eval()

var str = '{"foo": "bar"}';
var obj = eval('(' + str + ')');
log.info(obj.foo);

 

Option 2: Update Rhino engine and use JSON.parse()

  1. Download the latest version of Rhino from here:
    https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Download_Rhino
  2. In the <SoapUI>\lib folder, replace js-1.7R2.jar with js-<ver>.jar from the downloaded archive.
  3. Restart SoapUI.

Now you can use JSON.parse():

var str = '{"foo": "bar"}';
var obj = JSON.parse(str);
log.info(obj.foo);

Helen Kosova
SmartBear Technical Writer
________________________

 Join SmartBear Connect next April in Boston to meet with software development, API and testing experts from around the world! Get your ticket today!

View solution in original post

Valued Contributor

Re: How to parse JSON in javascript

Nice one - great answer! :-) 

Author of SoapUI Cookbook
Occasional Contributor

Re: How to parse JSON in javascript

Hello.

 

Thank you for your answer. You save me a lot of time.
It's exactly what I need.

Occasional Contributor

Re: How to parse JSON in javascript

Hello,

You can simply use the JavaScript function called JSON.parse() to convert text into javascript object.

E.x.

var txt = '{"name":"John", "age":30, "city":"New York"}'
var obj = JSON.parse(txt);