How do I grab the last portion of a log string and interpret it as json?

Zack

I am looking at a log message with the following format

datetime log_message_type message_type server {json_string}

So each line is separated by whitespace, always has the same fields for each line, and at the end has a json string with a variety of fields inside the json block.

I thought about doing this with a simple

with open('test.log', 'r') as f:
    for x in f:
        line = x.split()

        datetime         = line[0]
        log_message_type = line[1]
        message_type     = line[2]
        server           = line[3]
        json_string      = line[4]

This would have worked, except there are spaces in my json string, for example, something like this.

{ "foo" : "bar" }

So doing it in this way would split up my json string at the spaces. Is there any way I could use a regex or something to split on whitespace only until I get to the "json string" section of the line, and then preserve the rest of it? I tried doing something like

line = re.compile(".*\s.*\s.*\s.*\s").split(x)

To attempt to parse the line based on the 4 spaces before the json string portion, but I'm afraid I just don't know enough about how the regex system in python works. Could anyone give me a hand?

Edit : forgot to mention, I'm stuck with python 2.7 for this.

Noufal Ibrahim

Try something like this. Regular expressions can quickly get out of hand.

log_line = "datetime log_message_type message_type server {json_string}"
json_part = log_line.split(None, 4)[-1]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Grab a portion of List<string>

How do I "interpret" escaped characters in a string?

How do I use the substr function to grab the last 100 bytes of a 32K in length string?

How can I grab the index of where the longest identical consecutive portion of a string begins

How do I split string and return portion of string

How can I grab the last element of a List of string arrays?

How to remove the last portion of string in JavaScript?

How do I interpret ASCII values of characters of a string as chars in C?

How do I grab a specific sub-string of a file?

How do I grab each letter of a string in android?

Regex from a html parsing, how do I grab a specific string?

How do I split or grab a string in python after a comma in python

How do I interpret this instruction?

How do i interpret this timestamp

JSON has no identifiers, how do i grab info from it?

How do I return the local time portion of a string given a date string

how can i grab the last value of a coloumn

How do I remove a "base" portion of a path?

How do I get the last character of a string?

How do I remove the file suffix and path portion from a path string in Bash?

How do I select a portion of a string using regex and put it in a variable in Powershell

How do I grab the last X lines of a right triangle created using recursion?

How do I interpret the output of `cargo bench`?

How do I interpret Spark PCA output?

How do I interpret this python dependency tree?

How do I interpret the vertical line in a plot?

How do I interpret this JVM fault?

How do I interpret this header in this curl request?

How do I interpret the statistics of a memtest run?