Search code examples
pythonjsonformattingscreen-scraping

Scraped JSON data not in the same order as text data


I am trying to scrape data from www.crunchbase.com using their API. I have a very simple python script to get response from their API. When I write the json_data to a file the order in which the keys appear do not match with the order of the response I receive using the crunchbase API online. I have attached two files as an example. The company is "locaii" and one file shows the json data and the other simple text data. I know that order does not make a difference but yet how do i get the json data to match the same order??

The python code used is :-

page = requests.get("http://api.crunchbase.com/v/2/organization/locaii?user_key=<api_key>")
json_data = page.json()
open("locaii.txt","w").write(page.text)
out_file =open("locaii.json","w")
json.dump(json_data,out_file, indent=4)

The file "locaii.txt" is in this format - https://drive.google.com/file/d/0B8IDZa4NAwfqYzBSejJQbVJoV28/view?usp=sharing

The file "locaii.json" is in this format - https://drive.google.com/file/d/0B8IDZa4NAwfqdWtVUWVJaXBwNWM/view?usp=sharing


Solution

  • JSON objects (and the Python datatype to which they deserialize, dicts) are unordered. There is no guarantee as to what order the keys will end up in (or whether that order will remain the same across different versions/implementations of the language or even multiple runs of the same program), and you should never rely on it.