1

The problem is that there are multiple revision IDs and it only takes one revision ID no matter how many ever revisions are present. Using Dictionary along with JSON.

Need to fetch all revision tags present.

Data Structure:Structure of the JSON file

Code: #Defining a blank dictionary data = {} #File Loading Command with open(path+filename,encoding="iso-8859-1") as file: data = json.load(file) #Defining the Base Date to subtract from basedate = date(2006, 1, 1) #Number of Objects in JSON for count in range(140): #If there is any data in that then do the following if(data[count]): for each_item in data[count]: #If the item is revision if each_item == "revision": #This is where the problem lies since it always only fetches one revision time = data[count]["revision"]["timestamp"] currentdate = date(int(time[0:4]),int(time[5:7]),int(time[8:10])) #Calculating Days delta = currentdate - basedate print(data[count]["title"] + ": " +str(delta)) 

==================================Edit 1================================
The JSON is pretty big to display here, hence: https://api.myjson.com/bins/4sxm3

6
  • The keys in a Python dictionary must be unique. In other words, you can't have two or more items with the same key. I suggest that you make "revision" a list of dicts, and maybe rename it to "revisions". Alternatively, you could have keys like "revision0", "revision1", etc, but that wouldn't be pleasant to work with. Commented Mar 15, 2016 at 2:29
  • Actually, my 2nd suggestion may be the better option here, since you have no control over the creation of the JSON. As Paul Gowder mentions, you could modify the JSON text using regex substitution before you pass it Python's JSON parser. If you want help coding this it would be advisable to post some sample JSON data as text, rather than as a linked image. Commented Mar 15, 2016 at 2:43
  • 1
    This question shows how to parse such files using the json module: Python json parser allow duplicate keys. Commented Mar 15, 2016 at 3:02
  • Do you mean that I should read each line and if it is revision append it by revision1, revision2 and so on for each object? Commented Mar 15, 2016 at 3:02
  • Yes, that was my earlier suggestion, but take a look at the question I just linked to, which shows a better way. Commented Mar 15, 2016 at 3:05

1 Answer 1

2

Python dictionaries are like hashtables in other languages, keys are unique. It looks like you have multiple "revision" entries in JSON objects, and this is the problem. See this prior SO on the inadvisability of non-unique keys in JSON as well. Probably the best thing to do would be to reformat thbe JSON to make a list of revisions for each ID; not sure how to do this without something hackish like a regex replace...

Also, you don't need to pre-initialize a dictionary, the Python standard library JSON module will be smart enough to turn JSON objects into dicts on its own.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.