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.