How to extract json blocks from a textfile

Hey all!
I have a text file that has both "text" and json in the structure. The text has some output from the logger with date, time, message and log indicator along with an ID; The ID corresponds to the Json that follows the ID field. I have a Rexex that "works", but as I loop through the lines, it does not match multi-line blocks. - The regex does work for multi-line structures; but only if it is 1 string. I am trying to figure out either: Map, or dictionary? for the data for the regex to then compare to; or convert the entire file into a string. The File length does increase; so I am taking time into consideration.

Log Example:

[Logger]10/21/2019 MessageContent
==> Log.ID(22)
{
"params":{

"Location":[
6690,
2,
9020,
1
]
},
"OtherData""OtherData",
"MoreData:"MoreData2",
"MaybeAnotherArray:[922,2,22234,5,4342,23],
"ID": "22"
}


Every so often as the program advances it will add more data to the log file, So it is best to be able to find all instances. My goal is to place everything into 1 json file then parse with a C++ parser.
find a starting point, like "Log.ID(" or some other 'key' in the file that tells you where you are, and read strings with getline until you reach the closing bracket (you can do the zero sum algorithm for brackets, ++ for every { and -- for every } until zeroed) and concat all those strings into a single string then hit it with the regex. note that if you are removing the end of line markers as you read, you have to put them back in or change the regex!

or you can read the whole file as one big string and let the regex do its magic on parts of that, if it will fit in memory this is the way to go but log files could be huge(?).

a memory mapped file is very fast, even if you need to search it. Are these things significant in size vs your memory?
Last edited on
thanks @jonnin I managed to get the file into String Stream and let regex do its magic.

The regex's together should help with the data sorting and output. Now I think it all comes to logic.

i imagine that while (input >> sstr.rdbuf()); is keeping the program from stopping after the read. (open forever) where input is the file. What would be the best way to "stream" to a file until eof?

Gets the results and loops forever.


1
2
3
4
5
6
7
	while (std::regex_search(strang, m, reg)) {
		std::cout << "Results : \n" << m.str() << '\n';
		for (strang.length(); i > strang.length(); i++) {
			std::cout << m.str(i);
			i++;
		}
	}
Last edited on
Topic archived. No new replies allowed.