I have a json string that is truncated in google logs (for some reasons that is out of hand to fix)
the json string is is like 255000 chars, for simplicity consider the original json was like below
{ "a": "1",
"b" : [{
"b1": "<span>text b1</span>"
},
{
"b2": "<span>text b2</span>"
}],
"c": "3"
}
and truncated string comes as
{ "a": "1",
"b" : [{
"b1": "<spa... 34 characters truncated.
For sure if I try to JSON.parse above string I get an error
Now what I am thinking as solution is to make it a valid json with loosing some data so the output I am expecting is
{ "a": "1",
"b" : [{
"b1": "<spa"
}]
}
What is done above is
- removing
... 34 characters truncated.from end of string - closing the string by adding
"(in this case was needed but also might not be needed) - adding enough
]and}to make it valid object again
there are other cases to cover as well
for example
{ "a": "1",
"b" : [{
"b1": ... 34 characters truncated.
or
{ "a": "1",
"b" : [{
"b1 ... 34 characters truncated.
What is the best approach to do it?
I tried dirty-json library to fix but actually that doesn't help for this case