-2

I am working on a problem and my output is something like this: List with long string inside it

["21:15-21:30 IllegalAgrumentsException 1,
  21:15-21:30 NullPointerException 2,
  22:00-22:15 UserNotFoundException 1,
  22:15-22:30 NullPointerException 1
  ....."]

I need to convert the data in something like this:

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

How can I convert the data into this particular format ?

Error :

print(type(input)) // <class 'list'>
input = re.split(r',\s+', input[0])
print(input)  // ['21:15-21:30 IllegalAgrumentsException 1,
                   21:15-21:30 NullPointerException 2.....']

output = collections.defaultdict(collections.Counter)
for line in input:
    time, error, count = line.split(None, 2)
    output[time][error] += int(count)  //invalid literal for int() with base 10: '1
kelte
  • 27
  • 6
  • Python has a json library to convert lists into json, but to archive this particular format you posted you will have to parse your list beforehand, e.g. using regex. – T A Jul 31 '20 at 08:09

1 Answers1

4

I'm assuming your input list is actually a list of strings, but if it's not, you can split the string first:

import re
# If the input data is a list that has one entry, peel it off
input_data = input_data[0]
# Now we should have a string to split...
input_data = re.split(r',\s*', input_data)

As usual for a group-and-collate operation, collections.defaultdict (and in this particular summing case, collections.Counter too) come in handy:

import collections

input_data = [
    "21:15-21:30 IllegalAgrumentsException 1",
    "21:15-21:30 NullPointerException 2",
    "22:00-22:15 UserNotFoundException 1",
    "22:15-22:30 NullPointerException 1",
]

output = collections.defaultdict(collections.Counter)
for line in input_data:
    time, error, count = line.split(None, 2)
    output[time][error] += int(count)

response = [
    {
        "time": time,
        "logs": [
            {"exception": exception, "count": count}
            for (exception, count) in counter.items()
        ],
    }
    for (time, counter) in output.items()
]

print(response)

outputs (formatted)

[
    {
        "time": "21:15-21:30",
        "logs": [
            {
                "exception": "IllegalAgrumentsException",
                "count": 1,
            },
            {
                "exception": "NullPointerException",
                "count": 2,
            },
        ],
    },
    {
        "time": "22:00-22:15",
        "logs": [
            {
                "exception": "UserNotFoundException",
                "count": 1,
            }
        ],
    },
    {
        "time": "22:15-22:30",
        "logs": [
            {
                "exception": "NullPointerException",
                "count": 1,
            }
        ],
    },
]
AKX
  • 152,115
  • 15
  • 115
  • 172
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/218964/discussion-on-answer-by-akx-convert-list-to-a-particular-json-in-python). – Samuel Liew Jul 31 '20 at 09:17