Post

3 followers Follow
0
Avatar

Use json with Jython script

Hi There,

I have seen a few articles on this but I am not having success.  I have a jython script executing via xl-rules.   It requires me to read in some json, of couse when i said import json **it didn't resolve.    **How should I handle this?   I tried putting what I though was the standard python lib in the ext folder in a sub-directory, but it didn't work.  Any help would be great!

 

Thanks!

 

Bernie

 

Bernie Bonn Answered

Please sign in to leave a comment.

3 comments

0
Avatar

Hi Bernie

>  I have a jython script executing via xl-rules.   It requires me to read in some json, of couse when i said **import json **it didn't resolve.

Could you give us some more details of which steps you're using in the rule, or could you paste the rule definition itself here? I'm asking because if the step is running on a remote server, the libraries that are available will actually depend on whichever Jython version/configuration is being invoked there. As far as I recall, XL Deploy does not ship over an "on-demand" Jython setup, but uses whichever Jython the target middleware provides.

The team will hopefully be able to provide some more details!

Regards

Andrew

Andrew Phillips 0 votes
0
Avatar

Hi Bernie,

Here's a few different options to how you can do it.

Solution 1:

  1. You can keep the external module on a standard location across all machines in regards to if your jython step runs locally or remotely.
  2. In my case, i am running it on overthere.Localhost so i downloaded simplejson library and kept it under downloads /Users/amitmohleji/Downloads/simplejson-3.6.5
  3. Now in your associated script, you can type this 

import sys

sys.path.append("/Users/amitmohleji/Downloads/simplejson-3.6.5")

import simplejson as json

 

This made it work for me. For some reason, i couldn't get to work my python JSON library as it was giving me some error.

Solution 2: This is a last alternative

If in your jython script, you'll type this:

import os

print os._file_

  1. This will give you the location of your os library being used by jython.
  2. It comes out like $XLD_HOME/lib/jython-standalone-2.5.3.jar/Lib/os.py
  3. So now you may add your module to this jython jar file inside Lib folder and restart the server
  4. This should also make it work

 

Amit Mohleji 0 votes
0
Avatar

Hi guys, thank a lot for responding.  

I am attaching my xl rule.   I need to execute it locally on the XLD server so I choose a jython script step.   I put the simplejson folder in ext directory and refer to it this way(below) and it works.   Thanks again for the info!!

import simplejson as json

Is this the preferred method for adding python libraries for access in jython scripts?  

Here is the rule:

<rule name="WsdlRefresh" scope="deployed">  
           <conditions>  
                  <type>wls.WarModule</type>  
                  <operation>CREATE</operation>  
                 <operation>MODIFY</operation>  
                 <expression>deployed.layer7WsdlUri is not None</expression>  
    </conditions>  
          <steps>  
              <jython>  
                   <order>100</order>  
                   <description expression="true">'Refresh wsdl for %s' % deployed.name</description>  
                   <script-path>scripts/planning/refreshwsdl.py</script-path>  
                   <jython-context>  
                   <user>XebiaLabs</user>  
                   <app expression="true">deployedApplication</app>  
             </jython-context>  
      </jython>  
</steps>  
</rule>
Bernie Bonn 0 votes