Python Forum
JSON - Am I looking at an array already?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
JSON - Am I looking at an array already?
#1


Python newbie, please be gentle.
If you can point out what I'm looking at, I can hopefully self help from here in:

I'm attempting a task that will return a list of object ID's from a database via JSON, but in all the JSON reading I've done I still seem unable to discern what the pieces of the puzzle are that I'm looking at, at least by name, and more by function, and that's stopping me from getting further reading help / specs.

Here's an example:
{"objectIdFieldName":"OBJECTID","objectIds":[5834,5835,5836,5837,5838,5839,5840,5841,5842,5843]}

Am I looking at:
Object: Begins and ends with the curly braces {}, so the whole thing is the object
String: objectIdFieldName and associated value OBJECTID (So OBJECTID is the name of my DB table?)
String: objectIds and an associated [array of the id's]. I'm not sure of what to call the 'objectIds'. It's not a JSON object, what is it?

Assuming that the above is right, then I'm looking for a way to load the array of objectIds into a Python list where I can hopefully iterate through them.

I am getting hung up trying to return the objectIds in to a list - for example, in the below I'm not entirely sure how to return the objectIds just to the command window. How would I return the objectIds?
>>> import json
>>> import requests

>>> result = requests.get('https://Really_Long_Internal_URL&f=json').json()

>>> for x in result: #<-- This line and the next should return my array, eg 5834,5835,5836, but I'm not sure how
...     print (x)
...
objectIds  
objectIdFieldName
>>>
Thanks for any help you might be able to impart
Reply
#2
(Oct-23-2017, 09:26 AM)anakaine Wrote: I'm not sure of what to call the 'objectIds'. It's not a JSON object, what is it?
When use .json() you get back a Python dictionary with mixed in list.
>>> d = {"objectIdFieldName":"OBJECTID","objectIds":[5834,5835,5836,5837,5838,5839,5840,5841,5842,5843]}
>>> d['objectIds']                                                                                          
[5834, 5835, 5836, 5837, 5838, 5839, 5840, 5841, 5842, 5843]                                           
                                                                                                            
>>> # To iterate over id's                                                                                                         
>>> for item in d['objectIds']:                                                                             
...     print(item)                                                                                         
5834                                                                                                        
5835                                                                                                        
5836                                                                                                        
5837                                                                                                        
5838                                                                                                        
5839                                                                                                        
5840                                                                                                        
5841                                                                                                        
5842                                                                                                        
5843                                                                                                        
                                                                                                            
>>> # It's a Python list if you wonder                                                                                                            
>>> type(d['objectIds'])                                                                                    
<class 'list'>
Reply
#3
snippsat: Thank you very much. Thankyou both for letting me know it was a list, and also for showing how I could iterate over the list. Much obliged.

That last line, using 'type', is also a great tip.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with Json Array kwekey 2 1,665 Aug-02-2021, 05:11 PM
Last Post: kwekey
  convert a json file to a python dictionnary of array Reims 2 2,234 Sep-10-2019, 01:08 PM
Last Post: Reims
  Python convert csv to json with nested array without pandas terrydidi 2 9,652 Jan-12-2019, 02:25 AM
Last Post: terrydidi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020