reiley
reiley

Reputation: 3761

Python - Add multiple values in dictionary / json value

I'm building a python application which receives REST response in below format:

[
  {
    'metric': 'pass_status',
    'history': [
      {
        'date': '2019-02-20T10:26:52+0000',
        'value': 'OK'
      },
      {
        'date': '2019-03-13T11:37:39+0000',
        'value': 'FAIL'
      },
      {
        'date': '2019-03-13T12:00:57+0000',
        'value': 'OK'
      }
    ]
  },
  {
    'metric': 'bugs',
    'history': [
      {
        'date': '2019-02-20T10:26:52+0000',
        'value': '1'
      },
      {
        'date': '2019-03-13T11:37:39+0000',
        'value': '6'
      },
      {
        'date': '2019-03-13T12:00:57+0000',
        'value': '2'
      }
    ]
  },
  {
    'metric': 'code_smells',
    'history': [
      {
        'date': '2019-02-20T10:26:52+0000',
        'value': '0'
      },
      {
        'date': '2019-03-13T11:37:39+0000',
        'value': '1'
      },
      {
        'date': '2019-03-13T12:00:57+0000',
        'value': '2'
      }
    ]
  }
]

You can see dates are same within for each metric. I want to collate this data date-wise, i.e. my result json/dictionary should look like:

[
    '2019-02-20T10:26:52+0000' : {
        'pass_status' : 'OK',
        'bugs' : '1',
        'code_smells' : '0'
    },
    '2019-03-13T11:37:39+0000' : {
        'pass_status' : 'FAIL',
        'bugs' : '6',
        'code_smells' : '1'
    },
    '2019-03-13T11:37:39+0000' : {
        'pass_status' : 'OK',
        'bugs' : '2',
        'code_smells' : '2'
    }
]

What will be the suggested approach to do this?

Thanks

Upvotes: 1

Views: 166

Answers (1)

Adam.Er8
Adam.Er8

Reputation: 13403

I tried some itertools.groupby magic, but it turned into a mess...

maybe iteration + defaultdict is just keeping it simple...

like this:

from collections import defaultdict

result = defaultdict(dict)

for metric_dict in data:
    metric_name = metric_dict['metric']
    for entry in metric_dict['history']:
        result[entry['date']][metric_name] = entry['value']

print(dict(result))

or a full example with the data:

data = [
  {
    'metric': 'pass_status',
    'history': [
      {
        'date': '2019-02-20T10:26:52+0000',
        'value': 'OK'
      },
      {
        'date': '2019-03-13T11:37:39+0000',
        'value': 'FAIL'
      },
      {
        'date': '2019-03-13T12:00:57+0000',
        'value': 'OK'
      }
    ]
  },
  {
    'metric': 'bugs',
    'history': [
      {
        'date': '2019-02-20T10:26:52+0000',
        'value': '1'
      },
      {
        'date': '2019-03-13T11:37:39+0000',
        'value': '6'
      },
      {
        'date': '2019-03-13T12:00:57+0000',
        'value': '2'
      }
    ]
  },
  {
    'metric': 'code_smells',
    'history': [
      {
        'date': '2019-02-20T10:26:52+0000',
        'value': '0'
      },
      {
        'date': '2019-03-13T11:37:39+0000',
        'value': '1'
      },
      {
        'date': '2019-03-13T12:00:57+0000',
        'value': '2'
      }
    ]
  }
]

from collections import defaultdict

result = defaultdict(dict)

for metric_dict in data:
    metric_name = metric_dict['metric']
    for entry in metric_dict['history']:
        result[entry['date']][metric_name] = entry['value']

print(result)


Upvotes: 1

Related Questions