#C14971. CSV Action Counter

    ID: 44679 Type: Default 1000ms 256MiB

CSV Action Counter

CSV Action Counter

You are given a JSON formatted input via standard input. The JSON object includes a key actions that is an array of objects. Each object contains a key actions which is an array of action records. Each record consists of a string field type and an integer field count.

Your task is to parse the JSON input, aggregate the counts for each action type, and then output the results in CSV format to standard output.

The CSV output must have a header row with the two fields type and count, followed by one row for each action type. The rows should appear in the order in which the action type is first encountered in the JSON input.

For example, if the JSON input is:

{
  "userId": "12345",
  "actions": [
    {
      "category": "file",
      "actions": [
        {"type": "upload", "count": 5},
        {"type": "delete", "count": 2}
      ]
    },
    {
      "category": "email",
      "actions": [
        {"type": "send", "count": 10},
        {"type": "receive", "count": 15}
      ]
    }
  ]
}

Then the output should be:

type,count
upload,5
delete,2
send,10
receive,15

Note: You can assume that the input is correctly formatted. If there are no actions, your CSV output should only contain the header row.

In mathematical terms, if we denote the set of action types as \(T\) and the count of an action \(t \in T\) as \(c(t)\), then you are required to compute:

\[ \text{For each } t \in T, \quad c(t) = \sum_{a \in A(t)} a.count \]

inputFormat

The input consists of a single JSON string provided through standard input. The JSON object has at least the following structure:

{
  "userId": "...",
  "actions": [
    {
      "category": "...",
      "actions": [
        {"type": "...", "count": number},
        ...
      ]
    },
    ...
  ]
}

outputFormat

Output the aggregated action counts in CSV format to standard output. The first line must be the header:

type,count

Then, for each action type, output a new line in the format:

<action_type>,<total_count>

Output rows in the order in which the action types first appear in the input.

## sample
{"userId": "12345", "actions": [{"category": "file", "actions": [{"type": "upload", "count": 5}, {"type": "delete", "count": 2}]}, {"category": "email", "actions": [{"type": "send", "count": 10}, {"type": "receive", "count": 15}]}]}
type,count

upload,5 delete,2 send,10 receive,15

</p>