Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decoding json in jython using only default libraries

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.

like image 518
eric woodworth Avatar asked Jan 22 '14 16:01

eric woodworth


1 Answers

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.

like image 73
eric woodworth Avatar answered Sep 19 '22 17:09

eric woodworth