0

I have JavaScript that parses a JSON object (object has array) and returns the value from the ZONE field.

var obj = JSON.parse(json_text);
parsed_val = obj.features[0].attributes.ZONE

I would like to convert the JavaScript code to Jython.

This is what I've tried:

from com.ibm.json.java import JSONObject

obj = JSONObject.parse(json_text)
parsed_val = obj.get('features.attributes.ZONE'); 

The Jython compiles, but it doesn't return a valid value (it returns None). I think this is because I haven't referenced the array properly.

How can I parse the JSON object/array using Jython to get the ZONE value?

(Jython version is 2.7.0. However, I can't seem to use Python's JSON library (normally included in Jython)).

User1974
  • 276
  • 1
  • 17
  • 63
  • *I am unable to add additional Python libraries to my system/server. Therefore, I believe I am limited to the Java libraries that are included in Jython/Maximo.* – User1974 Aug 22 '19 at 22:03
  • 1
    You can add java libraries to the system but that creates other issues. Google the java docs for the functions associated. https://www.ibm.com/support/knowledgecenter/ja/SSEUEX_2.0.2/com.ibm.javaeuc.doc/com/ibm/json/java/JSONObject.html – Milton Aug 22 '19 at 22:37
  • 1
    Also, since it is obvious you are using a GIS endpoint, I would note that a lot can go wrong with your script if the GIS were to go down, move url, request gets lost, etc. I discourage url links to GIS data except when already interacting with it graphically. Always an exception though. – Milton Aug 22 '19 at 22:41
  • Try `.get('features').get('attributes').get('ZONE');` – vikarjramun Aug 24 '19 at 13:23
  • @vikarjramun Thanks, but what about the array/index position (`0`)? Doesn't the code need to know which thing in the array to get? – User1974 Aug 24 '19 at 13:29
  • 1
    @User1973 ah I didn't see that originally. I was just trying to translate what you included (`obj.get('features.attributes.ZONE')`) to something that would probably work. Idk how this specific json library works, maybe try `.get('features').get(0)...`, `.get('features')[0]...`. Either way, there will be a way to do it using this specific library, since JSON arrays are pretty common. – vikarjramun Aug 24 '19 at 16:35
  • @Milton I'm attempting to address your **GIS vulnerability concerns** over on Code Review (thanks): [Work order spatial query (Part 2)](https://codereview.stackexchange.com/questions/226786/work-order-spatial-query-part-2). – User1974 Aug 25 '19 at 17:36

1 Answers1

2

I needed to use get() at each level of the object.

As well as specify the array's index position after the first level: [0].

from com.ibm.json.java import JSONObject

obj = JSONObject.parse(json_text)
parsed_val = obj.get("features")[0].get("attributes").get("WEEK")

Credit goes to @vikarjramun for pointing me in the right direction. Thanks.

User1974
  • 276
  • 1
  • 17
  • 63
  • 1
    The first `get()` was JSONObject's or JSONArray's `get()` method inherited from `Hashtable` or `HashMap` (I forget which), not Python's, depending on what came back from the `JSONObject.parse()` call. It would only be Python's method if you had done something to cast or coerce `obj` into a Python object. – Preacher Aug 30 '19 at 06:27
  • 2
    Here are [IBM's JavaDocs for JSONObject](https://www.ibm.com/support/knowledgecenter/en/SSEUEX_2.0.2/com.ibm.javaeuc.doc/com/ibm/json/java/JSONObject.html). It extends java.util.HashMap. – Preacher Sep 03 '19 at 14:12