1

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

0

1 Answer 1

1

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)


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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.