简体   繁体   English

在 python 中将 json 从一种形式转换为另一种形式

[英]convert json from one form to another in python

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?如何在 python 中将日志合并在一起?

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.这个问题的其他部分一样,这将是defaultdict时间......但我们需要OrderedDict来保持原始时间戳顺序。

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},
        ],
    }
]

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
        }
      ]
    }
  ]
}

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},
        ],
    }
]
    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM