When first starting to work with json, we were receiving json communication from one of our vendors. So, the first thing I wanted to do was to be able to read it. I spent quite some time working on breaking it down in d3 and finally succeeded. Then sometime later, I found this Python function (json.dumps) that works great. Here is sample code which extracts the json and makes it readable. Check it out.
! This program demonstrates how raw json can be displayed
! in a readable format using the python json.dumps() module.
! I am only using the "indent" option with json.dumps().
!
! Over all steps
! 1. Read the json to be converted
! 2. Setup or read the python script
! 3. Swap in the json to the script
! 4. Open a Linux directory
! 5. Write the python script to the directory
! 6. Make the script executable
! 7. Execute the python script with output to the screen (may be captured)
! 8. Or write the reply to a file for visual review by editing it
!
! Take the json and set it to a variable (read from a file)
! This should be only in the first attribute.
raw.json = \{"name":"John","age":30,"married":True,"divorced":False,"children":("Ann","Billy"),"pets":None,"cars":[{"model":"BMW 230","mpg":27.5},{"model":"Ford Edge","mpg":24.1}]}\
!
! Body
!
! Python script (may be read from a file)
python = "#!/usr/bin/python2"
python<2> = \import json\
python<3> = \answer = (json.dumps(raw.json, indent=4))\
python<4> = \print (answer)\
python = swap(python,"raw.json",raw.json)
!
! Open the directory (this can be any directory that you have access to)
open "/tmp" to tmp.dir else crt "cannot open /tmp"; stop
!
! Write the script to the home directory
write python on tmp.dir,"ext.json.py"
!
! Make the script executable
execute "!chmod 755 /tmp/ext.json.py"
!
! Execute the script with output to the screen
execute "!/tmp/ext.json.py"
!
! Or, you may edit output for review
! Write the output to a file for viewing
execute "!/tmp/ext.json.py" capturing reply
write reply on tmp.dir,"reply.json"
!
! Edit the output
data "p"
execute "ed /tmp reply.json"
end
------------------------------
Ted Hurlbut
DBA
AMECO - American Equipment Co
Greenville SC US
------------------------------
Original Message:
Sent: 09-21-2022 07:16
From: Stefano Gallotta
Subject: JSON output
Hi all
Has 10.4 (or lower) got the ability (function) to create (and read) JSON ? (I know that OpenInsight has)
If not, is anyone prepared to share their version of such a function that they may have created?
I have written something to extract System Builder screens to convert them (into JSON) to HTML pages (using Python) but it may be interesting how others have approached this.
Thanks
------------------------------
Stefano Gallotta
Managing Member
Simply Red Open Systems
Milnerton ZA
------------------------------