import json in STAF/STAX

639 Views Asked by At

I have been working on STAF & STAX. My objective is to read a JSON file using STAF & STAX Return Testcase PASS or FAIL. I tried updating my staf to latest version with latest python version.

Python Version Detail

20130408-15:38:19   
Python Version : 2.5.2 (Release_2_5_2:7206, Mar 2 2011, 23:12:06) 
[Java HotSpot(TM) Client VM (Sun Microsystems Inc.)]

Here is my Code:

try:
      import simplejson as json
    except ImportError:
      import json
  title = [] 
  album = []
  slist = []
  json_data=open('d:\Json_File.txt')
  data = json.load(json_data)
  for i in range(data["result"].__len__()):
      title = data["result"][i]["Title"]
      album = data["result"][i]["Album"]
      slist = data["result"][i]["Title"] + ' [' + data["result"][i]["Album"] + '] \n'

It is giving error given below

20130408-11:32:26   STAXPythonEvaluationError signal raised. Terminating job. 
===== XML Information =====
File: new13.xml, Machine: local://local 
Line 15: Error in element type "script".

===== Python Error Information =====
com.ibm.staf.service.stax.STAXPythonEvaluationException: 
Traceback (most recent call last):
  File "<pyExec string>", line 1, in <module>
ImportError: No module named simplejson

===== Call Stack for STAX Thread 1 =====[
  function: main (Line: 7, File: C:\STAF\services\stax\samples\new13.xml, Machine: local://local)
  sequence: 1/2 (Line: 14, File: C:\STAF\services\stax\samples\new13.xml, Machine: local://local)
]

What's the process to include JSON in STAF Module.

2

There are 2 best solutions below

0
On

I have little idea about STAF/STAX. But going by what the error says, it seems simplejson module is not available. Rewrite the import line as following:

try:
  import simplejson as json
except ImportError:
  import json

You can fallback to json module in case import fails (Python 2.6+).

0
On

STAX uses Jython (a version of Python written in Java), not Python, to execute code within a element in a STAX job. As i said i was using the latest version of STAX, v3.5.4, then it provides an embedded Jython 2.5.2 (which implements the same set of language features as Python 2.5) to execute code within a element.

Note: Jython 2.5.2 does not include simplejson since simplejson is included in Python 2.6 or later.

Appendix F: "Jython and CPython Differences" in the STAX User's Guide at talks about some differences between Jython and Python (aka CPython). Installing Python 2.7 or later in system will have no effect on the fact that STAX uses Jython 2.5.2 to execute code within a element in a STAX job. However, "simplejson can be run via Jython." I added the directory containing the simplejson module to the sys.path within my STAX job and then import simplejson.
For example:

<script> 
  myPythonDir = 'C:/simplejson'   

  import sys 
  pythonpath = sys.path 

  # Append myPythonDir to sys.path if not already present 
  if myPythonDir not in pythonpath: 
    sys.path.append(myPythonDir) 

  import simplejson as json 
</script> 

Or, if you want to use Python 2.7 or later that you installed on your system (that includes simplejson), you can run a Python script (that uses json) via your STAX job using a** element.

For example, to use Python 2.7 (if installed in C:\Python2.7) to run a Python script named YourPythonScript.py in C:\tests.

<process> 
  <location>'local'</location> 
  <command mode="'shell'">'C:/Python2.7/bin/python.exe YourPythonScript.py'</command> 
  <workdir>'C:/tests'</workdir> 
</process>