Use json.org on SoapUI groovy - json

I'm trying to use org.json on groovy SoapUI to convert a JSON string into XML.
But When I'm trying to perform my groovy script (this one)
import org.json.JSONObject
import org.json.XML
def str = "{'name':'JSON','integer':1,'double':2.0,'boolean':true,'nested':{'id':42},'array':[1,2,3]}";
JSONObject json = new JSONObject(str);
String xml = XML.toString(json);
log.info(xml)
I'm getting an error "unable to resolve class org.json.JSONObject , line 1, column 1.
I downloaded the jar on http://mvnrepository.com/artifact/org.json/json/20141113 , I put it on SoapUI/bin/ext but It's not working.
Can someone please help me with this?

Coping the json-20141113.jar in SOAPUI_HOME\bin\ext works for me, and your code executes correctly, logging this:
Thu Feb 12 21:58:53 CET 2015:INFO:<boolean>true</boolean><array>1</array><array>2</array><array>3</array><double>2.0</double><name>JSON</name><integer>1</integer><nested><id>42</id></nested>
Note that in order to load the libraries you must restart SOAPUI, maybe this is causing your error.
Note also that the jar you linked is compiled with java 1.8 so check if you run SOAPUI using this version of java if not then you will receive Unsupported major.minor version 52.0 error. I say this because SOAPUI is available to download with or without java; if you download it with java the version is java 1.7. If this is the case you must download java 1.8 and modify SOAPUI_HOME\bin\soapui.bat to use the correct version.
Hope this helps,

Related

Flask TypeError: Object of type Decimal is not JSON serializable

We are using flask for our application service. Internally we are using Jsonify to return the response and our response contains decimal data.
Couple of things to mention here.
We are using Windows machine
Some of my colleagues they are not getting any issue with the data with same configuration.
One thing we observed is flask by default uses simplejson if installed. If not, then it will
fall back to json package.
Sample data : jsonify({'response':Decimal('12345.00000')})
Our question is why for some people flask by default using simplejson and for others it's not able to identify simplejson(even though installed) and using only json throwing decimal json error.
Any idea what might be the root cause and what change needs to be done, let flask know to use simplejson over json ?
For time being we did below manual change in flask package.
Instead of jsonify({'response': Decimal('12345.00000')}) can't you just use jsonify({'response': float('12345.00000')})?
is there any chance you're using different versions of flask?
https://github.com/pallets/flask/issues/3555
in version 2, support was dropped for simplejson, but as noted in the comments, you can make it use simplejson for the JSONEncoder:
from simplejson import JSONEncoder
app.json_encoder = JSONEncoder
it also looks as though there is a fix for the decimal issue that was PR'ed but is not yet released, and will be released in version 2.0.2

JSON path extractor throws error when added to sampler

I am trying to extract a token from response received in json. I have used jpgc-json-2.1 as well as jpgc-json-2.6 json plugin with JMeter. I am using JMeter version 2.13.
When I try to add a post processor to an HTTP sampler, I get this error in message box: cannot find class:com/jayway/jsonpath/PathNotFoundException
I have added jmeter-plugins-cmn-jmeter-0.3.jar to lib and jmeter-plugins-json-2.6.jar and jmeter-plugins-manager-0.11.jar to ext folder.
Any pointers would be appreciated.
Thanks
If you have to use JMeter 2.13 for some reason, you need to get the matching JSON Plugins version, in your case it would be JMeterPlugins-ExtrasLibs-1.4.0.zip which you will need to install manually.
Be aware that JMeter 2.13 is a little bit outdated, it was released in March 2015 so it is 2+ years old for the moment so I would recommend updating to the latest JMeter version (JMeter 3.2 as of now). Newer JMeter versions normally have new features (for instance JSON Extractor), bug fixes and performance improvements.
Since JMeter 3.0, there is a native JSON extractor by default.
I would highly suggest you use JMeter 3.2 and this one.
No need for plugins here.

Unable to use "as JSON" after upgrading to grails 2.1.1 from grails 1.3.4

I'm in the process of upgrading a grails plugin from 1.3.4 to grails 2.1.1. After upgrading I now have an integration test that fails that was not failing before. It fails on using the "as JSON" (grails.converters.JSON).
#Test
public void testConvertCollectionOfEnvironmentSettingsToJSON() {
EnvironmentSetting setting = configurationService.getEnvironmentSetting('ENFORCE_SCHEMA_INSTANCE_RULE')
def jsonSetting = setting as JSON //exception thrown here
def s = jsonSetting as String
assertNotNull jsonSetting
}
The exception and stacktrace:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'com.company.ipsvc.configuration.domain.EnvironmentSettingAllRevs#48c12420' with class 'com.company.ipsvc.configuration.domain.EnvironmentSettingAllRevs' to class 'grails.converters.JSON'
at com.company.ipsvc.configuration.converter.json.basic.BasicEnvironmentSettingJSONIntegrationTests.testConvertCollectionOfEnvironmentSettingsToJSON(BasicEnvironmentSettingJSONIntegrationTests.groovy:28)
I am able to use encodeAsJSON() successfully. I also have the same issue with as XML.
I think converters (as JSON syntax) will only work on domain objects and collections by default.
To convert arbitrary objects you should use the encodeAsJSON() converter, I believe. Or use an object marshaller, where you tell the converter how to deal with your object.
The docs aren't very clear on this though..
See:
http://grails.org/Converters+Reference (object marshalling section at bottom)
http://grails.org/doc/latest/ref/Plug-ins/codecs.html
But I note that http://grails.org/doc/latest/api/grails/converters/JSON.html#JSON%28java.lang.Object%29 says that the object converts POGOs.. Maybe it means if you have a marshaller?
I did find this reference too:
Notice that the ‘as’ operator is not overloaded for plain objects ...
Domain objects can use the ‘as’ operator to cast an object to JSON, the same as a collection. So unlike POGOs, where they must be massaged into a list or have encodeAsJSON explictly called ...
http://manbuildswebsite.com/2010/02/08/rendering-json-in-grails-part-2-plain-old-groovy-objects-and-domain-objects/
Which seems to describe the situation.
For non-Domain objects, we found that this would crop up when running tests... the solution for us was to use new JSON:
render new JSON( obj )
This would allow the test to work, and the code does the same thing (essentially)
Ran into a similar issue that broke unit test using grails 2.2.1 . At issue was a straight obj as JSON conversion attempt. But this was interpreted as type casting instead.
The workaround is to stuff your obj to be converted into a map like this [data:obj] as JSON

Parsing and Writing JSON using Ant

Has anyone managed to parse and write JSON in a task in Ant?
I need to parse a json file, modify it and then write it back on disk. I managed to parse it using the rhino engine from JDK 6 but i'm stuck because I don't know how to serialize it back to disk.
It seemes I need a JSON serializer, rhino does not apparently include one.
Recent versions of Rhino should have a JSON object: see this bug that was resolved as fixed in 2010, and the actual class is called NativeJSON and has a stringify method that should let you retrieve a JSON string.
JSON.stringify is available with Rhino 1.7R3 onwards as part of the ECMAScript 5 work.
See: https://developer.mozilla.org/En/New_in_Rhino_1.7R3

How to parse the JSON response in Blackberry/J2ME?

I want to parse the response coming from the server in JSON format. I have done some googling but i can't find any library or jar kind of thing.
Everywhere there is provided open source code as zip file.
How can i achieve this? if there is no jar available for blackberry then how to use that open source code in my application??
There is an open source JSON for J2ME API on Mobile and Embedded Application Developers Project
Also you can download JSON ME (Zip file) at JSON.org not supported anymore. But you can get it from here.
I believe you can simply copy content of json project src folder to your Blackberry project src folder, refresh it in eclipse package explorer and build it.
See for details: Using JavaScript Object Notation (JSON) in Java ME for Data Interchange
I am developing a Blackberry client application and I confronted the same problem. I was searching for JSON way of parsing the response which I get from the server. I am using Eclipse plug-in for BB as IDE and it comes with BB SDK including the ones for JSON.
The easiest answer to this question is that:
Initially do not forget to use this import statement:
import org.json.me.JSONObject;
Then assign your JSON formatted response from the server to a String variable:
String jsonStr = "{\"team\":\"Bursaspor\",\"manager\":\"Ertuğrul Sağlam\",\"year\":\"2010\"}";
Create a JSONObject:
JSONObject obj = new JSONObject(jsonStr);
i.e. if you want to use the value of the "team" field which is "Bursaspor" then you should use your JSONObject like this:
obj.getString("team")
This call will return the string value which is "Bursaspor".
P.S: I inspired this solution from this site which simply explains the solution of the same problem for Android development.
http://trandroid.com/2010/05/17/android-ile-json-parse-etme-ornegi-1/
When you got response string then use this code
try {
JSONObject jsonres = new JSONObject(jsons);
System.out.println("Preview icon from jsonresp:"+jsonres.getString("mydata"));
} catch (JSONException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
one thing jsons is your response string which is in json format & mydata your key of data so you can get your data from JSONObject. Thnks
chek this post to find out how to get JSONME source from SVN:
http://www.java-n-me.com/2010/11/how-to-import-source-code-from-svn-to.html
Hope it helps someone.
You can also try :
http://pensivemode.fileave.com/verified_json.jar
I was also looking for a jar version of json (to link along my lib) :
https://radheshg.wordpress.com/tag/json/
but it seems not portable :
Building example
C:\Program Files (x86)\Research In Motion\BlackBerry JDE 4.5.0\bin\rapc.exe -quiet import="C:\Program Files (x86)\Research In Motion\BlackBerry JDE 4.5.0\lib\net_rim_api.jar";lib\${PROJECT}.jar;lib\json.jar codename=example\example example\example.rapc warnkey=0x52424200;0x52525400;0x52435200 Y:\src\${PROJECT}-java.git\example\src\mypackage\MyApp.java Y:\src\${PROJECT}-java.git\example\src\mypackage\MyScreen.java
tmp3139/org/json/me/JSONArray.class: Error!: Invalid class file: Incorrect classfile version
Error while building project
http://supportforums.blackberry.com/t5/Java-Development/JSON-library/m-p/573687#M117982
So far JSON.simple is looking like a great viable option.
JSONParser parser=new JSONParser();
System.out.println("=======decode=======");
String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
Object obj=parser.parse(s);
JSONArray array=(JSONArray)obj;
System.out.println("======the 2nd element of array======");
System.out.println(array.get(1));
System.out.println();