3

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?

1
  • 2
    w3schools.com/python/python_json.asp the text you just described was JSON. API calls usually give you back JSON. You first need to de serialize that then i believe it turns into a list that you can manipulate from there. Commented Nov 20, 2018 at 15:50

2 Answers 2

2

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'))

Sign up to request clarification or add additional context in comments.

Comments

2

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)

2 Comments

good catch. if thats the cause the OP should probably be careful with the replace though. Basically a regex that makes sure it’s not part of a string. but... i would have expected the python side json module to know about false/False
Indeed a regex solution would be safer. The Json module doesn't handle well variations such as case sensitivity @JLPeyret! @RyanAdams could tell if it fits, and in positive case I can update the code to add the regex solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.