Return number of objects based on created date from a list of nested JSON objects

Question:

I have a list of JSON objects and want to return a dictionary with the count of objects based on created[year-month] where created[year-month] is the key and count the value.

I tried:

result = {}
for record in data_in:
    if record['created'][:7] not in result:
        result[record['created'][:7]] = {"created": 0}
    result[record["created"][:7]]["created"] += 1
        
result

And code produces the below output:

{'2020-03': {'created': 1},
 '2020-04': {'created': 1},
 '2020-01': {'created': 3}}

But my desired out is:

{
  '2020-03': 1, 
  '2020-04': 1, 
  '2020-01': 3
}

I know I’m missing something out but can’t figure out what. Can some one help refactor the code, or provide a better approach to get the desired output?

Note:

  1. Additional imports are not allowed.

JSON file

data_in =   [
    {
        "package": "FLEXIBLE",
        "created": "2020-03-10T00:00:00",
        "summary": [
            {
                "period": "2019-12",
                "documents": {
                    "incomes": 63,
                    "expenses": 13
                }
            },
            {
                "period": "2020-02",
                "documents": {
                    "incomes": 45,
                    "expenses": 81
                }
            }
        ]
    },
    {
        "package": "ENTERPRISE",
        "created": "2020-04-19T00:00:00",
        "summary": [
            {
                "period": "2020-01",
                "documents": {
                    "incomes": 15,
                    "expenses": 52
                }
            },
            {
                "period": "2020-02",
                "documents": {
                    "incomes": 76,
                    "expenses": 47
                }
            }
        ]
    },
    {
        'package': 'FLEXIBLE',
        'created': '2020-01-15T00:00:00',
        'summary': [
            {
                'period': '2020-03',
                'documents': {
                    'incomes': 39, 
                    'expenses': 48
                }
            },
            {
                'period': '2020-04', 
                'documents': {
                    'incomes': 76, 
                    'expenses': 20
                }
            }
        ]
    },
    
    {
        'package': 'INTERNAL',
        'created': '2020-01-07T00:00:00',
        'summary': [
            {
                'period': '2019-12',
                'documents': {
                    'incomes': 4, 
                    'expenses': 53
                }
            },
            {
                'period': '2020-01', 
                'documents': {
                    'incomes': 60, 
                    'expenses': 48
                }
            },
            {
                'period': '2020-02', 
                'documents': {
                    'incomes': 88, 
                    'expenses': 85
                }
            },
            {
                'period': '2020-03', 
                'documents': {
                    'incomes': 84, 
                    'expenses': 81
                }
            }
        ]
    },
    {
        'package': 'ENTERPRISE',
        'created': '2020-01-03T00:00:00',
        'summary': [
            {
                'period': '2020-04',
                'documents': {
                    'incomes': 27, 
                    'expenses': 13
                }
            }
        ]
    }]
Asked By: Mar3eczek17

||

Answers:

If I understand your problem correctly, you can just modify your code to increment the counter directly instead of storing this value in a dictionary.

result = {}
for record in data_in:
    if record['created'][:7] not in result:
        result[record['created'][:7]] = 1
    else:
        result[record["created"][:7]] += 1

Note that your solution assumes that the created date will always be in YYYY-MM-... format.

Is this what you are looking for?

Answered By: Pawel Kam

This approach will count objects in data_in based on 'created' date. record['created'][:7] will extract YYYY-MM from 'created' string

new_dict = {}
for record in data_in:
    created = record['created'][:7]
    if created not in new_dict:
        new_dict[created] = 0
    new_dict[created] += 1
print(new_dict)

{'2020-03': 1, '2020-04': 1, '2020-01': 3}
Answered By: Jamiu S.
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.