Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help Parsing JSON
#1
Can someone please help me parse this json object?

{
    "12345": {
        "2021-01-20": {
            "a": 1,
            "b": 3,
            "c": 0
		},
        "2021-01-21": {
            "a": 1,
            "b": 3,
            "c": 0
		}
	},
    "12346": {
        "2021-01-20": {
            "a": 1,
            "b": 3,
            "c": 0
		},
        "2021-01-21": {
            "a": 1,
            "b": 3,
            "c": 0
		}
	}
}
The first number is a product ID, to which there are many.
The second is a date, to which there will be thousands.
Then I want to grab the key and value within each date.

I am looking to get Product ID, Date, A, B, and C into python variables.

I am just really struggling with this.
Reply
#2
you can easily iterate over dict of dicts of dicts. the real question is what you are going to do with the data
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jan-25-2021, 01:12 PM)buran Wrote: you can easily iterate over dict of dicts of dicts. the real question is what you are going to do with the data

I tried this...

for product in json:
    print(product)
    for date in product:
        print(date)
This shows the product ID as I expected.
But the date under products, doesn't

I'm probably going to stuck in into a DB for now.
Reply
#4
You can loop.

data = {
    "12345": {
        "2021-01-20": {
            "a": 1,
            "b": 3,
            "c": 0
        },
        "2021-01-21": {
            "a": 1,
            "b": 3,
            "c": 0
        }
    },
    "12346": {
        "2021-01-20": {
            "a": 1,
            "b": 3,
            "c": 0
        },
        "2021-01-21": {
            "a": 1,
            "b": 3,
            "c": 0
        }
    }
}


for customer_id, dates in data.items():
    for date, values in dates.items():
        for key, value in values.items():
            print(customer_id, date, key, value, sep=", ")
            # here is maybe one level too deep
            # depends on your task
Or you can access data directly with subscription:

value = data["12345"]["2021-01-20"]["c"]
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
don't use json as name, it's a built-in module and you don't want to override it

data = {
    "12345": {
        "2021-01-20": {
            "a": 1,
            "b": 3,
            "c": 0
        },
        "2021-01-21": {
            "a": 1,
            "b": 3,
            "c": 0
        }
    },
    "12346": {
        "2021-01-20": {
            "a": 1,
            "b": 3,
            "c": 0
        },
        "2021-01-21": {
            "a": 1,
            "b": 3,
            "c": 0
        }
    }
}

for product, dates in data.items():
    for adate, values in dates.items():
        print(f"{product},{adate},{values['a']},{values['b']},{values['c']}")
Output:
12345,2021-01-20,1,3,0 12345,2021-01-21,1,3,0 12346,2021-01-20,1,3,0 12346,2021-01-21,1,3,0
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(Jan-25-2021, 01:44 PM)buran Wrote: don't use json as name, it's a built-in module and you don't want to override it

Here a small example, what happens:

import json

print(json) # <-- shows information where the module is located

json = [1, 2, 3] # <-- assign a list to the name `json`
print(json) # <-- Print the object

# now trying to use a function from json module
json.dumps([1,2,3]) # <-- AttributeError
Output:
<module 'json' from 'c:\\users\\andre\\appdata\\local\\programs\\python\\python39\\lib\\json\\__init__.py'> [1, 2, 3]
Error:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-1-e9bf17c080eb> in <module> 5 json = [1, 2, 3] 6 print(json) ----> 7 json.dumps([1,2,3]) # <-- AttributeError AttributeError: 'list' object has no attribute 'dumps'
If you import json, then the module json is loaded and assigned to the name json on module level.

The exception happens because the name json was assigned to something else.
In this case, the list has no attribute named dumps. dumps is a function from the json module, but not from a list. And this happens if you don't take care of names and assigning names which already exists.

You can even do this with built-in functions, which leads into unexpected behavior.

To avoid this, don't reassign objects to the name of imported modules and built-in function.
List of built-in functions: https://docs.python.org/3/library/functions.html
If you use an IDE, often these words are highlighted, if you assign accidentally a new object.
For example, sum is also a built-in, but often in code the programmers assign a value to sum and later they want to use the built-in sum function and BOOM.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Parsing large JSON josvink66 5 627 Jan-10-2024, 05:46 PM
Last Post: snippsat
  Parsing JSON pyStund 4 2,965 Jul-31-2022, 02:02 PM
Last Post: pyStund
  Json Parsing sshree43 5 1,780 May-04-2022, 09:21 PM
Last Post: snippsat
  json api data parsing elvis 0 924 Apr-21-2022, 11:59 PM
Last Post: elvis
  string indices must be integers when parsing Json ilknurg 3 6,345 Mar-10-2022, 11:02 AM
Last Post: DeaD_EyE
  Parsing JSON with backslashes bazcurtis 3 9,315 Feb-08-2020, 01:13 PM
Last Post: bazcurtis
  JSON parsing (nested) fakka 0 3,065 Nov-25-2019, 09:25 PM
Last Post: fakka
  Parsing json - dictionary with in dictionary krish216 6 3,629 Jul-30-2019, 10:01 PM
Last Post: cvsae
  JSON Parsing PythonLearner007 3 2,520 Jan-31-2019, 10:47 AM
Last Post: gontajones

Forum Jump:

User Panel Messages

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