So i have this Whatsapp log file that looks like this: (Timestamps are not acurate)
[15.11.16, 16:13:29] Person A: Hello
[15.11.16, 16:13:45] Person B: This is
a multiline
message
[15.11.16, 16:14:12] Person A: Wow, so cool
I want to parse them into a json format that would look like that:
{
"msg": [
{
"sender": "Person A",
"message": "Hello",
"time": 1479205511000,
"response": {
"sender": "Person B",
"message": "This is\na multiline\nmessage",
"time": 1479205536000
}
},
...
]
}
Whats hapening is, first of all the message will be split in three parts:
- Time (As a timestamp)
- Name of the Person
- Message (If it is a multiline message, the newline characters should be escaped)
The other thing thats happening is the response thing, which chooses one Main Person (in this case "Person A"). Then it looks through each message starting at the Top, looking for the first message by that person (Called "Main-Message"). If it finds one, it will look for the response of the other person (The next message (Called "Response")). If the next message is from the main person again, it will choose that message as the "Main-Message" and look for the next one.
Example:
Person A: Hi
Person B: Hi
Person A: This is cool
Person A: I am cool
Person B: Ah ok
In this example, we will choose "Person A" as our Main-Person. The first message is written by our Main-Person, so it will be our Main-Message. The next message is from the other Person, so it will be our Response.
{
"sender":"Person A",
"message":"Hi",
"time":1479205511000,
"response":{
"sender":"Person B",
"message":"Hi",
"time":1479205536000
}
}
The next message is from our Main-Person again, so we will take it as our Main-Message. But the message after that is not by the other Person so we will skip the message before. The message after that is from our opponent again, so it will be our Response.
{
"sender":"Person A",
"message":"I am cool",
"time":1479205511000,
"response":{
"sender":"Person B",
"message":"Ah ok",
"time":1479205536000
}
}
We now have a JSON like that:
{
"msg":[
{
"sender":"Person A",
"message":"Hi",
"time":1479205511000,
"response":{
"sender":"Person B",
"message":"Hi",
"time":1479205536000
}
},
{
"sender":"Person A",
"message":"I am cool",
"time":1479205511000,
"response":{
"sender":"Person B",
"message":"Ah ok",
"time":1479205536000
}
}
]
}
There are a couple Problems:
- Multiline Messages
- Non UTF-8 Characters like '' that should be escaped -> Text Encoding
- Huge Logs (up to 100.000 lines) -> Performance
- Sometimes the lines start with control characters so something like String.startsWith not always works perfectly
As you might've guessed by my tags, the language im using is Java.
If there are any questions or unclear information, feel free to draw my attention to it.