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

Viv
  • 59
  • 5
  • 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 `dict`s, and maybe rename it to "revisions". Alternatively, you _could_ have keys like "revision0", "revision1", etc, but that wouldn't be pleasant to work with. – PM 2Ring Mar 15 '16 at 02: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. – PM 2Ring Mar 15 '16 at 02:43
  • 1
    This question shows how to parse such files using the `json` module: [Python json parser allow duplicate keys](http://stackoverflow.com/q/29321677/4014959). – PM 2Ring Mar 15 '16 at 03: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? – Viv Mar 15 '16 at 03:02
  • Yes, that was my earlier suggestion, but take a look at the question I just linked to, which shows a better way. – PM 2Ring Mar 15 '16 at 03:05
  • That new JSON you linked to doesn't have multiple "revision" keys in one `dict`, though. – PM 2Ring Mar 15 '16 at 03:11

1 Answers1

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.

Community
  • 1
  • 1
Paul Gowder
  • 2,409
  • 1
  • 21
  • 36