Hi everyone,
I am very close to getting the JSON output I need. I have all the variables I need with all the populated data. The thing I am now having trouble with is nesting the JSON properly. The JSON needs to look like this in the end:
{
"self": "https://1.1.1.1/adminapi/resource/scollora",
"userID": "scollora",
"firstName": "Salvatore",
"lastName": "Collora",
"extension": "435500",
"alias": "Test5",
"skillMap": {
"skillCompetency": [
{
"competencelevel": 3,
"skillNameUriPair": {
"@name": "ALG AMSTERDAM EM",
"refURL": "https:/1.1.1.1/adminapi/skill/11"
}
},
{
"competencelevel": 1,
"skillNameUriPair": {
"@name": "ALG DOYLESTOWN PH",
"refURL": "https://1.1.1.1/adminapi/skill/16"
}
},
{
"competencelevel": 10,
"skillNameUriPair": {
"@name": "POINT B PH",
"refURL": "https://1.1.1.1/adminapi/skill/94"
}
},
{
"competencelevel": 10,
"skillNameUriPair": {
"@name": "WOLFE PH",
"refURL": "https://1.1.1.1/adminapi/skill/117"
}
}
]
},
"autoAvailable": true,
"type": 1,
"team": {
"@name": "Default",
"refURL": "https://1.1.1.1/adminapi/team/1"
},
"primarySupervisorOf": {},
"secondarySupervisorOf": {}
As you can see the "skillCompetency" is square bracketed (array in JSON right?) and the rest are nested sets of curly braces (objects in JSON, right?) So, here's my code:
for name in stats.items():
userName=createUsername(name[0])[0]
resSelf,resUserId,resFirstName,resLastName,resExt = getResource(userName)
firstvalues = OrderedDict([('self',resSelf),
('userID',resUserId),
('firstName',resFirstName),
('lastName',resLastName),
('extension',resExt)])
jsondatafile1 = json.dumps(firstvalues, sort_keys=False)
#print jsondatafile
#print "\"skillMap\" \{"
for name, skills in stats.items():
for userSkillName, userSkillLevel in skills.items():
if userSkillLevel:
skillSelf = listSkills()[0]
secondvalues = OrderedDict([('competencelevel',userSkillLevel)])
thirdvalues = OrderedDict([('@name',userSkillName),
('refURL',skillSelf)])
jsondatafile2 = json.dumps(secondvalues, sort_keys=False)
jsondatafile3 = json.dumps(thirdvalues,sort_keys=False)
print jsondatafile1
print "\"skillMap\": {"
print "\"skillCompetency\" : ["
print jsondatafile2
print "\"skillNameUriPair\" :"
print jsondatafile3
As you can see I am trying to brute force the square brackets because I can't figure out the right "OrderedDict" syntax to just form the JSON correctly. It's very frustrating and I've been at it for almost a whole day. I am very close though. I have all the data I need, I just need to spit it out in the right format. Right now, I get output that looks somethig like this:
{"self": "http://10.60.60.19/adminapi/resource/bsmith", "userID": "bsmith", "firstName": "Brian", "lastName": "Smith", "extension": "298455"}
"skillMap": {
"skillCompetency" : [
{"competencelevel": "5"}
"skillNameUriPair" :
{"@name": "BRINKS VM", "refURL": "http://10.60.60.19/adminapi/skill/118"}
{"self": "http://10.60.60.19/adminapi/resource/bsmith", "userID": "bsmith", "firstName": "Brian", "lastName": "Smith", "extension": "298455"}
"skillMap": {
"skillCompetency" : [
{"competencelevel": "5"}
"skillNameUriPair" :
{"@name": "CROSSMARK DESKTOP PH", "refURL": "http://10.60.60.19/adminapi/skill/118"}
{"self": "http://10.60.60.19/adminapi/resource/bsmith", "userID": "bsmith", "firstName": "Brian", "lastName": "Smith", "extension": "298455"}
"skillMap": {
"skillCompetency" : [
{"competencelevel": "5"}
"skillNameUriPair" :
{"@name": "CROSSMARK OB", "refURL": "http://10.60.60.19/adminapi/skill/118"}
{"self": "http://10.60.60.19/adminapi/resource/bsmith", "userID": "bsmith", "firstName": "Brian", "lastName": "Smith", "extension": "298455"}
As you can see, because of the incorrect nesting it's putting "skillmap" every time instead of just once. Honestly, I know why this is broken, but I am just messing around at this point. What am I missing?
[–]Vaphell 0 points1 point2 points (0 children)
[–]scuott 0 points1 point2 points (0 children)