0

I'm trying to find a full (i.e. exact) json body in a log file. Let's say we have body like:

{
  "superObject": {
        "object": {
          "property1": 0,
          "property2": "",
          "property3": False
        }
      },
      "superProperty1": "abcd",
      "superProperty2": 4321
    }

and a .txt file, named log_test.txt and it contains:

bla
bla 
bla 
POST   {
  "superObject": {
        "object": {
          "property1": 0,
          "property2": "",
          "property3": False
        }
      },
      "superProperty1": "abcd",
      "superProperty2": 4321
    }
bla bla
bla 

I am trying to find all the body in log_text.txt.

What I tried

First option was to make body as str(dict), split it by lines and the every req line find line by line in file:

import sys
import json
import string

def test():        
    req = {
      "superObject": {
        "object": {
          "property1": 0,
          "property2": "",
          "property3": False
        }
      },
      "superProperty1": "abcd",
      "superProperty2": 4321
    }

    for req_line in str(req).splitlines(True):
        with open("C://log_test.txt", 'r') as log:
            for line in log:
                if req_line in line:
                    print req_line + " It's there!!!"
                    log.close()
                    return
        log.close()            
        print "It's NOT there!!!"
        raise Exception("String '" + req_line + "' does not exist")


if __name__ == "__main__":
    test()

No luck though, it didn't split it, and tried to search all the body in every line, the same if to for req_line in str(req).split("\n").

Second option was to dump dict via python's json lib and also look for line by line: for req_line in json.dumps(req).splitlines(True): looks for all body in each file's line, for req_line in json.dumps(req): looks for first { finds it and returns, i.e. passes just first line. I tried to search similar problem, but found just solutions similar to my first option than didn't work, as I wrote.

Ffisegydd
  • 51,807
  • 15
  • 147
  • 125
German Petrov
  • 1,475
  • 1
  • 17
  • 21

2 Answers2

1

The problem with option one is that str(req) doesn't insert new lines into the string. You could try to load the log file and then normalize the log file and the req string. I tested it with the following and it found the body:

import sys
import json
import string

def test():        
    req = {
        "superObject": {
            "object": {
                "property1": 0,
                "property2": "",
                "property3": False
            }
        },
        "superProperty1": "abcd",
        "superProperty2": 4321
      }

    with open("./log_test.txt", 'r') as log:
        log_string = ''
        for line in log:
            log_string = log_string + line.replace('\n', '').replace(' ', '')
        req_string = str(req).replace('\n', '').replace(' ', '').replace("'", '"')
        log.close()            
        if req_string in log_string:
            print str(req) + " It's there!!!"
        else:
            print "It's NOT there!!!"
            raise Exception("String '" + str(req) + "' does not exist")


if __name__ == "__main__":
    test()
CubeRZ
  • 574
  • 5
  • 9
  • tried your code, got raise Exception("String '" + str(req) + "' does not exist") Exception: String '{'extensions': {'object': {'property1': 0, 'property2': '', ' property3': False}}, 'superProperty1': 'abcd', 'superProperty2': 4321}' does not exist but thanks anyway :) maybe it's related to line-breaks parsing differences between windows (i'm on it) and linux? – German Petrov Sep 05 '14 at 12:52
  • Line endings should be the problem. You can try to add a .replace('\r', '') along with the rest. – CubeRZ Sep 05 '14 at 12:57
1

Well, if you only want to find that json body (exact) in one txt file like log_test.txt, it's simple. Following your previous code:

req = '''{
  "superObject": {
        "object": {
          "property1": 0,
          "property2": "",
          "property3": False
        }
      },
      "superProperty1": "abcd",
      "superProperty2": 4321
    }'''


def test():

    with open("C://log_test.txt", 'r') as log:
        _file = log.read()
        if req in _file:
            print req + " It's there!!!"
            log.close()
            return
        log.close()
        print "It's NOT there!!!"
        raise Exception("String '" + req + "' does not exist")


if __name__ == "__main__":
    test()

It works for your example. If you'd like parser a full JSON file, take a look at Parsing values from a JSON file in Python

Community
  • 1
  • 1
fenix688
  • 2,435
  • 2
  • 17
  • 23
  • hm, tried exact your code, got: raise Exception("String '" + req + "' does not exist") Exception: String '{ "extensions": { "object": { "property1": 0, "property2": "", "property3": False } }, "superProperty1": "abcd", "superProperty2": 4321 }' does not exist, but thanks anyway :) – German Petrov Sep 05 '14 at 12:51
  • sorry, the error of course is raise Exception("String '" + req + "' does not exist") Exception: String '{ "superObject": { "object": { "property1": 0, "property2": "", "property3": False } }, "superProperty1": "abcd", "superProperty2": 4321 }' does not exist – German Petrov Sep 05 '14 at 13:01