Reputation: 31
I am trying to create a list from an API call, the data comes out like this which I believe is bytes.
Nothing sensitive below here:
[{"Udid":"00000004324234235","SerialNumber":"SN42A32A34","MacAddress":"00127F287220","Imei":"","EasId":"443243434225346FE9","AssetNumber":"","DeviceFriendlyName":"00:16:7F:18:7C:B0 ","LocationGroupId":{"Id":{"Value":544},"Uuid":"364243f-97342b-4287-9333-b942442313","Name":"Location2"},"LocationGroupName":"Location2","UserId":{"Id":{"Value":5111},"Uuid":"364243f-97342b-4287-9333-b942442313","Name":"MDM Enroll"},"UserName":"Enroll123","UserEmailAddress":"[email protected]","Ownership":"C","PlatformId":{"Id":{"Value":10},"Name":"PalmPhone"},"Platform":"PalmPhone","ModelId":{"Id":{"Value":44},"Name":"PalmPhone - X101Nitro"},"Model":"PalmPhone - X101Nitro","OperatingSystem":"1.7.44444","PhoneNumber":"","LastSeen":"2018-11-11T07:46:57.257","EnrollmentStatus":"Enrolled","ComplianceStatus":"Compliant","CompromisedStatus":false,"LastEnrolledOn":"2018-11-09T18:26:03.610","LastComplianceCheckOn":"0001-01-01T00:00:00.000","LastCompromisedCheckOn":"2018-06-13T19:28:22.933","IsSupervised":false,"DeviceMCC":{"SIMMCC":"","CurrentMCC":""},"VirtualMemory":0,"IsDeviceDNDEnabled":false,"IsDeviceLocatorEnabled":false,"IsCloudBackupEnabled":false,"IsActivationLockEnabled":false,"IsNetworkTethered":false,"IsRoaming":false,"SystemIntegrityProtectionEnabled":false,"ProcessorArchitecture":0,"Id":{"Value":140},"Uuid":"364243f97342b42879333-b942442313"},
Then it repeats with the same format just different data. I tried doing something along the lines of (ast.literal_eval(b"myList"))
(myList being the variable that holds all of the above).
Any ideas?
Upvotes: 3
Views: 84
Reputation: 1424
The problem with your API return, is that it's a list and the dictionary from the first element, contains some fields as "false", but they are not encapsulated with "" as strings, and python only recognizes "False", with capital F.
So you should first convert the response to text, then replace the false
for False
, and as last point use json.loads to use the Json on your code: (where api_return
is what your receive)
response = str(api_return[0])
response.replace("false", "False")
response_json = json.loads(response)
Upvotes: 2
Reputation: 835
If the returned string is valid json, you can process it like so:
import json
x = json.loads(myList)
x
will then contain the list of dictionaries that the API call returned.
ast.literal_eval(myList)
should also work - if myList is actually of type bytes
like you indicated, try ast.literal_eval(myList.decode('UTF-8'))
Upvotes: 2