8

I have a question about getting json into my jython script. Here's my scenario:

  • I am running a python app on my laptop
  • That app needs to share data with a jython app running in a hosted environment out in a 3rd party's cloud environment.
  • I have no ability to add 3rd party modules to this environment (so I can't install com.xhaus.jyson for example)
  • This probably means I'm limited to features that are native to java - org.json.JSONObject perhaps

So with those limitations, I want to take a dictionary object on my laptop, turn it into json, deliver it to the hosted jython app and then use the native jython or java tools to turn it back into that dictionary object so I can continue working on it in my script hosted in the cloud.

I already know how to do this in "regular" python. It's easy. import json and go nuts. But my java kung fu is weak and I've never worked in jython before.

So I'm trying to figure out if this if it's possible to do this reliably and easily using the java underlying the jython or if I'd be better off using something like ast and just send the dictionary as a string literal. I'd honestly prefer to stick with json for all the normal reasons people like json so any help with leveraging the java libraries to do this work would be appreciated.

eric woodworth
  • 163
  • 2
  • 10

6 Answers6

3

I forgot about this question. My core problem here was that I was using a 3rd party cloud offering and they were the owners of the Jython install so I was limited in what I could change about the Jython environment. At the time I was thinking I could leverage a JAVA library that would be available to jython to solve this problem but that never worked out.

While jython was out of my control I did control how I sent the data so instead of using JSON, I sent formatted strings and then used the python ast library, which was in jython, to turn those strings into python objects.

In the end it looked something like this:

thestring = """['name', 'quest', 'favorite color']"""
theobject = ast.literal_eval(thestring)

That type of logic let my python script on my local machine post strings to a web app running jython and convert those strings into python data types and then use them. It was exactly what I wanted to do with JSON without actually using JSON - it was python dicts so it looked an awful lot like JSON if you weren't really paying attention.

Thanks everybody for your suggestions.

eric woodworth
  • 163
  • 2
  • 10
0

You could use Jackson or GSON. You might use anything listed on JSON.org under Java, you might be able to use the stuff under "Python" (for example simplejson).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

You could use simplejson, which can be used as pure python, so will run on Jython. By including it in the same source folder as your other code, there's no need for a special installation.

Dan Getz
  • 8,774
  • 6
  • 30
  • 64
  • The entire environment where my script is run is abstracted away from where I load my script. Includes to relative paths will break because my script is run elsewhere and not where I upload it. – eric woodworth Jan 22 '14 at 16:44
  • So your script can consist of only one file? – Dan Getz Jan 22 '14 at 17:13
  • seems that way. I'm new to this service but that appears to be the case. So I can import whatever is default to jython. That means I cannot import json like I would in python. But it does mean I can leverage underlying java libraries. I am terrible with java however, which is why I use Python, so having access to java isn't helping me that much. – eric woodworth Jan 22 '14 at 17:52
  • If you're using the latest version of jython, you have access to the python json library. So, see my answer below.... – hd1 Nov 04 '15 at 17:42
0

Jyson seems to be an open source project implementing a python complaint json codec in pure Java.

Download it here: http://opensource.xhaus.com/projects/jyson/files.

Then unzip and add jyson-1.0.2/lib/jyson-1.0.2.jar to your CLASSPATH.

Then import like this:

 import com.xhaus.jyson.JysonCodec as json

Found this info here: http://aholzner.wordpress.com/2010/07/31/using-json-from-jython/. Works for me.

Tim Ludwinski
  • 2,704
  • 30
  • 34
-1

This question is a bit dated, but I came across this when having a similar issue:

https://support.xebialabs.com/hc/communities/public/questions/201998425-Use-json-with-Jython-script

In essence, this is how I solved it (using simplejson):

try:
    sys.path.append('<PATH TO SIMPLEJSON ROOT>')
    import simplejson as json
except Exception, e:
    print e
Tom
  • 414
  • 4
  • 17
-1

If you're using the latest version of jython, you have access to the python json library. So:

import json
mydict_as_json = json.dumps(mydict)
# send over the wire
# on the remote side
import json
mydict = json.load(mydict_as_json_from_remote_as_file_like_object)

Hope that helps...

hd1
  • 33,938
  • 5
  • 80
  • 91