UOPY Example - Working with JSON
Making the connection
The following code is a simple example to connect to a local U2 Database. In this example I am connecting to UniData, but the code would work the same against UniVerse, the only thing you would need to modify is the server and account settings.
import uopy
import getpass
user = input("Enter user name? ")
passwd = getpass.getpass("Enter Password? ")
def makeconnection(user, password):
config = {
'user': user,
'password': passwd,
'service': 'udcs',
'account': 'C:\\\\U2\\\\ud82\\\\XDEMO',
'encoding': 'GB18030',
'pooling_on': 'False'
}
thissession = uopy.connect(**config)
return thissession
session = makeconnection(user, passwd)
print(str(session))
Enter user name? mrajkowski
Enter Password? ········
<uopy._session.Session object {'host': 'localhost', 'account': 'C:\\\\U2\\\\ud82\\\\XDEMO', 'port': 31438, 'timeout': 100, 'service': 'udcs', 'encoding': 'GB18030', 'ssl': False, 'user': 'mrajkowski', 'password': ''} at 0x000001D44571DB80>
Read in a JSON document
Note that to in this example, I am limiting my discussion on how to create and consume JSON. I am using the TOJSON keyword to generate the JSON from a U2 File, in this case the MEMBERS file in the XDEMO account.
import pprint
cmd = uopy.Command("LIST MEMBERS FIRST_NAME LAST_NAME CITY STATE ZIP SAMPLE 3 TOJSON")
st = cmd.run()
out = cmd.response
print(out)
{
"MEMBERS":[
{"_ID" : "0011","FIRST_NAME" : "Dwayne","LAST_NAME" : "Kennedy","CITY" : "Bristol","STATE" : "Idaho","ZIP" : "33563"
},
{"_ID" : "0384","FIRST_NAME" : "Mitra","LAST_NAME" : "Bruce","CITY" : "St. Paul","STATE" : "Alaska","ZIP" : "66340"
},
{"_ID" : "0757","FIRST_NAME" : "Steve","LAST_NAME" : "Seagle","CITY" : "Edmunston","STATE" : "West Virginia","ZIP" : "51671"
}
]
}
Converting the string to a Python Dictionary with eval() function
Note that while this is an easy way, it could be exploited by hackers.
data = eval(out)
print(type(data))
pprint.pprint(data)
<class 'dict'>
{'MEMBERS': [{'CITY': 'Bristol',
'FIRST_NAME': 'Dwayne',
'LAST_NAME': 'Kennedy',
'STATE': 'Idaho',
'ZIP': '33563',
'_ID': '0011'},
{'CITY': 'St. Paul',
'FIRST_NAME': 'Mitra',
'LAST_NAME': 'Bruce',
'STATE': 'Alaska',
'ZIP': '66340',
'_ID': '0384'},
{'CITY': 'Edmunston',
'FIRST_NAME': 'Steve',
'LAST_NAME': 'Seagle',
'STATE': 'West Virginia',
'ZIP': '51671',
'_ID': '0757'}]}
As you can see with one command, I changed the string to a python dictionary object. The problem arises when you are not in control of what is being passed into the eval function.
code_string = 'print("This is an example of the eval() function executing commands.")'
eval(code_string)
This is an example of the eval() function executing commands.
Using the Python json module
A better way of dealing with the json string is to marshell it to a Python dictionary with the built-in json module.
import json
try:
jdata = json.loads(out)
print(type(jdata))
pprint.pprint(jdata)
except:
print("problem with json")
<class 'dict'>
{'MEMBERS': [{'CITY': 'Bristol',
'FIRST_NAME': 'Dwayne',
'LAST_NAME': 'Kennedy',
'STATE': 'Idaho',
'ZIP': '33563',
'_ID': '0011'},
{'CITY': 'St. Paul',
'FIRST_NAME': 'Mitra',
'LAST_NAME': 'Bruce',
'STATE': 'Alaska',
'ZIP': '66340',
'_ID': '0384'},
{'CITY': 'Edmunston',
'FIRST_NAME': 'Steve',
'LAST_NAME': 'Seagle',
'STATE': 'West Virginia',
'ZIP': '51671',
'_ID': '0757'}]}
Note that by using the json.loads method, we prevent a non-json string from causing a problem.
try:
jdata = json.loads(code_string)
print(type(jdata))
pprint.pprint(jdata)
except:
print("problem with json")
problem with json
Once you have the json, you can display some of the information
mydict = dict()
for each in jdata["MEMBERS"]:
print(each["_ID"] + " is for " + " " + each["LAST_NAME"] + ", " + each["FIRST_NAME"])
mydict[each["_ID"]] = each
0011 is for Kennedy, Dwayne 0384 is for Bruce, Mitra 0757 is for Seagle, Steve
Lets look at one of the Python Dict objects a bit closer
this_obj = mydict["0011"]
pprint.pprint(this_obj)
print()
print("With the Python dict object, we can access each named-value pair, for example just the CITY:")
print(this_obj["CITY"])
{'CITY': 'Bristol',
'FIRST_NAME': 'Dwayne',
'LAST_NAME': 'Kennedy',
'STATE': 'Idaho',
'ZIP': '33563',
'_ID': '0011'}
With the Python dict object, we can access each named-value pair, for example just the CITY:
Bristol
Preparing for writing to a new U2 file
Step 1: Get the Keys and the Values Step 2: Create the file
keys = mydict.keys()
values = mydict.values()
print(keys)
print(values)
dict_keys(['0011', '0384', '0757'])
dict_values([{'_ID': '0011', 'FIRST_NAME': 'Dwayne', 'LAST_NAME': 'Kennedy', 'CITY': 'Bristol', 'STATE': 'Idaho', 'ZIP': '33563'}, {'_ID': '0384', 'FIRST_NAME': 'Mitra', 'LAST_NAME': 'Bruce', 'CITY': 'St. Paul', 'STATE': 'Alaska', 'ZIP': '66340'}, {'_ID': '0757', 'FIRST_NAME': 'Steve', 'LAST_NAME': 'Seagle', 'CITY': 'Edmunston', 'STATE': 'West Virginia', 'ZIP': '51671'}])
Create the U2 file and dictionary ( Note this is a UniData example )
file_needed = 0
filename = "NEWMEMBERS"
try:
file = uopy.File(filename)
dfile = uopy.File(filename, uopy.DICT_FILE )
print("file opened")
except:
print("The file " + filename + " does not exist")
file_needed = 1
try:
if file_needed:
cmd = uopy.Command("CREATE.FILE " + filename + " 3 11")
cmd.run()
print("file created")
file = uopy.File("DATASOURCE")
dfile = uopy.File(filename, uopy.DICT_FILE )
print("now the file is opened")
except:
print("still an issue with the file")
print(type(file))
file opened <class 'uopy._file.File'>
Create U2 Dictionary items
Note this will step through the keys extracted in the last section and create d-type dictionary items.
one_obj = list(values)[0]
print(type(one_obj))
one_set_of_keys = one_obj.keys()
cnt = 0
for each_key in one_set_of_keys:
cnt += 1
print(each_key + " will be location " + str(cnt))
rec = [ "D", cnt, "", each_key, "10L", "S"]
dfile.write(each_key, rec)
cmd = uopy.Command("LIST DICT " + filename)
cmd.run()
print(cmd.response)
<class 'dict'> _ID will be location 1 FIRST_NAME will be location 2 LAST_NAME will be location 3 CITY will be location 4 STATE will be location 5 ZIP will be location 6 LIST DICT NEWMEMBERS BY TYP BY @ID TYP LOC CONV NAME FORMAT SM ASSOC 13:10:25 Nov 18 2021 1 @ID............ TYP LOC.......... CONV NAME........... FORMAT SM ASSOC..... @ID D 0 NEWMEMBERS 10L S CITY D 4 CITY 10L S FIRST_NAME D 2 FIRST_NAME 10L S LAST_NAME D 3 LAST_NAME 10L S STATE D 5 STATE 10L S ZIP D 6 ZIP 10L S _ID D 1 _ID 10L S 7 records listed
Now load the data
For this example, I am only loading the first object to the file
this_key = list(keys)[0]
the_fields = list(one_obj.keys())
the_values = [ list(one_obj.values()) ]
print(the_fields)
print(the_values)
st = file.write_named_fields(this_key, the_fields, the_values)
# Note 'ALL' is only in UniData
cmd = uopy.Command("LIST " + filename + " ALL")
cmd.run()
print(cmd.response)
['_ID', 'FIRST_NAME', 'LAST_NAME', 'CITY', 'STATE', 'ZIP'] [['0011', 'Dwayne', 'Kennedy', 'Bristol', 'Idaho', '33563']] LIST NEWMEMBERS ALL 13:21:45 Nov 18 2021 1 NEWMEMBERS _ID....... FIRST_NAME LAST_NAME. CITY...... STATE..... ZIP....... 0011 0011 Dwayne Kennedy Bristol Idaho 33563 1 record listed
------------------------------
Mike Rajkowski
support
Rocket Internal - All Brands
DENVER CO US
------------------------------
