0

I have got a response after altering a list. My response looks something like this :

{
 "response": [
    {
     "timestamp": "21:15-21:30",
     "logs": [
         {
             "exception": "IllegalAgrumentsException",
             "count": 1
         }
      ]
    },
    {
        "timestamp": "21:15-21:30",
        "logs": [
         {
             "exception": "NullPointerException",
             "count": 2
         }
     ]
   },..

I want the result to be something like this:-

 "response": [
     {
       "timestamp": "21:15-21:30",
       "logs": [
          {
              "exception": "IllegalAgrumentsException",
              "count": 1
          },
          {
              "exception": "NullPointerException",
              "count": 2
          } ]
   }

How can I merge logs together like above in python?

naive_user
  • 119
  • 8

3 Answers3

2

As with the other part of this question, it would be defaultdict time... but we'll need OrderedDict here to keep the original timestamp order.

import collections

input_data = [
    {
        "timestamp": "21:15-21:30",
        "logs": [{"exception": "IllegalAgrumentsException", "count": 1}],
    },
    {
        "timestamp": "21:15-21:30",
        "logs": [{"exception": "NullPointerException", "count": 2}],
    },
]

logs_by_timestamp = collections.OrderedDict()

for datum in input_data:
    logs_by_timestamp.setdefault(datum["timestamp"], []).extend(datum["logs"])

output_data = [
    {"timestamp": timestamp, "logs": logs}
    for (timestamp, logs) in logs_by_timestamp.items()
]

print(output_data)

outputs (formatted)

[
    {
        "timestamp": "21:15-21:30",
        "logs": [
            {"exception": "IllegalAgrumentsException", "count": 1},
            {"exception": "NullPointerException", "count": 2},
        ],
    }
]
AKX
  • 152,115
  • 15
  • 115
  • 172
  • `list indices must be integers or slices, not str` error at line `for datum..` – naive_user Jul 31 '20 at 12:16
  • Nope, that error doesn't come from this code. If your list is still in that `"response": [...]` thing, you'll naturally have to unwrap that first: `input_data = response["response"]` or whatnot. – AKX Jul 31 '20 at 12:18
  • yes, I saw that. But my order of data is changed totally. I want my order of output data to remain same input data – naive_user Jul 31 '20 at 12:25
  • Edited to use `OrderedDict` instead. – AKX Jul 31 '20 at 12:35
1

considering your situatuation..

check out the below patch

_dict = {
    "response": [
        {
            "timestamp": "21:15-21:30",
            "logs": [
                {
                    "exception": "IllegalAgrumentsException",
                    "count": 1
                }
            ]
        },
        {
            "timestamp": "21:15-21:30",
            "logs": [
                {
                    "exception": "NullPointerException",
                    "count": 2
                }
            ]
        }]}

_final = {}
for _dict in _dict.get('response'):
    if not _dict.get('timestamp') in _final:
        _final[_dict.get('timestamp')] = {
            'timestamp': _dict.get('timestamp'),
            'logs': []
        }
    _final[_dict.get('timestamp')]['logs'] += _dict.get('logs')

_result ={'response': list(_final.values())}

print(_result)

which will print ...

{
  'response': [
    {
      'timestamp': '21:15-21:30',
      'logs': [
        {
          'exception': 'IllegalAgrumentsException',
          'count': 1
        },
        {
          'exception': 'NullPointerException',
          'count': 2
        }
      ]
    }
  ]
}
Zaki Ahmed
  • 146
  • 1
  • 4
0

response = [
    {
        "timestamp": "21:15-21:30",
        "logs": [{"exception": "IllegalAgrumentsException", "count": 1}],
    },
    {
        "timestamp": "21:15-21:30",
        "logs": [{"exception": "NullPointerException", "count": 2}],
    },
]
final = []
for k,v in groupby(response, lambda x:x.pop('timestamp')):
    final.append({
        'timestamp':k,
        'logs':reduce(
        lambda x,y:  {'logs':y['logs']+x['logs']},      
        [*v]
        )['logs']
    })
print(final)
Output 
[
    {
        "timestamp": "21:15-21:30",
        "logs": [
            {"exception": "IllegalAgrumentsException", "count": 1},
            {"exception": "NullPointerException", "count": 2},
        ],
    }
]
    
Sagar Adhikari
  • 1,312
  • 1
  • 10
  • 17