1

I have a partial JSON object - it's a log that got terminated too early. I can see tools like vscode etc. can parse the partial JSON - eg, the top of the vscode window shows:

file.js > item > subitem > subitem > 3 > subitem

when I'm inside a nested object.

How I can automatically terminate the unterminated JSON? I could obviously manually match every unclosed bracket but given the size of the object that's obviously time consuming.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
  • Please try this one: https://stackoverflow.com/questions/546433/regular-expression-to-match-balanced-parentheses - good luck, Mike – mplungjan Sep 14 '21 at 09:09
  • 1
    I have also tried: HJSON which can't parse the truncated object. – mikemaccana Sep 14 '21 at 09:18
  • 1
    There's also https://stackoverflow.com/questions/67953019/parse-broken-json-string-to-json-object but it's specific type of JSON (JSON5) rather than a truncated object. – mikemaccana Sep 14 '21 at 09:20
  • Can you not just analyse the root cause of _why_ the JSON is being truncated rather than go through the hassle of trying to fix it? Do you need all of it? Assuming this is from an API can you pass it down in batches and then thread it together? – Andy Sep 14 '21 at 09:21
  • @Andy that's a really good question! It's being truncated due to AWS CloudWatch log size limits. I've updated the lambda to remove one of the larger keys, to reduce the size of the log nd get it under the limit. However being able to read the existing log would save having to wait for the logged event to reoccur. – mikemaccana Sep 14 '21 at 09:23
  • I'm not familiar with CloudWatch. Are you using the Insights tech? ["CloudWatch Logs Insights can extract a maximum of 1000 log event fields from a JSON log.](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_AnalyzeLogData-discoverable-fields.html). For extra fields that are not extracted, you can use the parse command to parse these fields from the raw unparsed log event in the message field." No idea if this is helpful. – Andy Sep 14 '21 at 09:32
  • Not sure of specific tools, but as a general approach: a *streaming JSON parser* to read the data as far as available, and either just print it while reading it, or construct a new JSON stream from it with a streaming JSON writer which can simply close all unclosed elements once you're done supplying data. – deceze Sep 14 '21 at 09:48
  • Sadly the 'regex' answer above is untenable, for the reasons given in the second answer (which consists of writing your own tool). – mikemaccana Sep 14 '21 at 11:08

0 Answers0