Cannon,

The way your script is setup it looks like you are passing it one or more file 
names or paths. That will work, but if you want to pass it data directly you 
could pass in a json string and then read the command line arguments by 
importing the sys module. Then sys.argv will be a list (python equivalent to a 
4D array) of passed in values with the zeroth element being the name of the 
script itself (so skip that one).

# -------------------------------------------
import sys  # import sys module
my_json = sys.argv[1] # set my_jason equal to the first non-script name 
parameter
# -------------------------------------------

If you need to get fancier with parsing arguments passed in you can also look 
at argparse.

Once you are done doing your parsing instead of (or in addition to) your print 
statement I think you want to use sys.stdout.write().

Maybe like this:
# -------------------------------------------
sys.stdout.write(json.dumps(jsonObj, sort_keys=True, indent=2))
sys.exit(0)  # 0 = no errors
# -------------------------------------------

To call your script I would do this:

// -------------------------------------------
C_TEXT($tIn;$tOut) // $tIn is blank, stdout will end up in $tOut
Launch External Process("python yourscript.py jsonfilepath_or_text";$tIn;$tOut)
// -------------------------------------------

I did a quick test (Windows) and this works for me.  I'd also be remiss if I 
didn't recommend using Python 3 instead of Python 2.

Let me know if I can be of more help,

Joshua

Joshua Hunter
[email protected]
(425)673-1974
www.dwdev.com
Dataworks Development, Inc.
Providing secure and configurable data management solutions for research and 
clinical labs since 1987.


-----Original Message-----
From: 4D_Tech [mailto:[email protected]] On Behalf Of Cannon Smith 
via 4D_Tech
Sent: Wednesday, February 15, 2017 11:57 AM
To: 4D iNUG Technical
Cc: Cannon Smith
Subject: LEP and Python Script

I’ve been given a python script which takes JSON and returns JSON with all the 
keys alphabetized which is helpful for comparing revisions in version control. 
However, I can’t figure out how to use LAUNCH EXTERNAL PROCESS and pass some 
JSON to it and get the result back. If anyone here knows how it can be done, 
I’d appreciate the help (I know nothing about python). MacOS if it matters.

Here is the script:

#!/usr/bin/python
import fileinput
import json
if __name__ == "__main__":
  jsonStr = ''
  for a_line in fileinput.input():
    jsonStr = jsonStr + ' ' + a_line.strip()    
  jsonObj = json.loads(jsonStr)  
  print json.dumps(jsonObj, sort_keys=True, indent=2) 

--
Cannon.Smith
Synergy Farm Solutions Inc.
Hill Spring, AB Canada
403-626-3236
<[email protected]>
<www.synergyfarmsolutions.com>


**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[email protected]
**********************************************************************
**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[email protected]
**********************************************************************

Reply via email to