Analysis of JSON server logs

PHP Medium json

Task «Analysis of JSON server logs»

Write a PHP program that reads from standard input a JSON string containing an array of server log objects. Each object has the fields: 'timestamp' (a string in ISO 8601 format), 'level' (a string: 'info', 'warning', 'error'), 'message' (a string). It is necessary to process the logs and output a JSON array of objects that are 'error' and occurred after 2023-06-15T00:00:00Z. For each such log, add a 'processed' field with the current date and time in ISO 8601 format (e.g., '2023-06-20T12:34:56Z'). Keep the remaining fields unchanged. Output the result as a JSON string, sorted by the 'timestamp' field in ascending order.

Input Format

One line — a JSON array of log objects.

Output Format

One line — a JSON array of filtered and supplemented objects.

Examples

Example 1

INPUT
[{"timestamp":"2023-06-14T10:00:00Z","level":"error","message":"Disk full"},{"timestamp":"2023-06-16T08:30:00Z","level":"error","message":"Connection timeout"},{"timestamp":"2023-06-15T12:00:00Z","level":"warning","message":"High memory usage"}]
OUTPUT
[{"timestamp": "2023-06-16T08:30:00Z", "level": "error", "message": "Connection timeout", "processed": "2026-05-17T10:07:20Z"}]

Example 2

INPUT
1,2,3
OUTPUT
[]

Example 3

INPUT
[{"timestamp":"2023-06-15T00:00:01Z","level":"error","message":"Oops"},{"timestamp":"2023-06-14T23:59:59Z","level":"error","message":"Late"}]
OUTPUT
[{"timestamp": "2023-06-15T00:00:01Z", "level": "error", "message": "Oops", "processed": "2026-05-17T10:07:20Z"}]

Example 4

INPUT
4,5,6
OUTPUT
[]
main.php