How to fix truncated string json to be able to parse it again

Viewed 1076

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

2 Answers

npm package untruncate-json working as expected

import untruncateJson from "untruncate-json";

const string = ` { "a": "1", 
   "b" : [{
      "b1": "<spa... 34 characters truncated.`;

// remove non-json ending
const truncatedJson = string.replace(/\.\.\. \d+ characters truncated\.$/,'');

// run library
const untruncatedJson = untruncateJson(truncatedJson);

console.log(untruncatedJson);

output:

 { 
   "a": "1", 
   "b" : [
     { "b1": "<spa" }
    ]
  }

I'm afraid you'll have to do it by yourself. But your approach sounds like a good start.

Related