How to fix a wrong JSON?

Asked

Viewed 194 times

-1

Next, I’m developing a Python program that collects data from a JSON and transfers it to a CSV file.

However, the server routine brings me a JSON with the wrong structure at some points of it, as in this case:

...
"Campo": valor,
"Campo": "valor"
},
[
{}
][
{
  "Campo": valor,
  "Campo": {},
...

When I run the program (which works perfectly with JSON files formatted correctly) with this JSON, it returns me the error:

  File "C:\Users\mpolillo\Documents\json-extraction\script\fix_json.py", line 17, in fix_test
    content = json.loads(content)
  File "C:\Users\mpolillo\AppData\Local\Continuum\anaconda3\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Users\mpolillo\AppData\Local\Continuum\anaconda3\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\mpolillo\AppData\Local\Continuum\anaconda3\lib\json\decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 123944 column 2 (char 3653439)

I mean, you’re warning me on the line where this mistake is. I wanted to know if there is any way to rewrite this JSON without having to do it manually or without having Python rewrite everything in hand and then write it in a new file or on top of it.

  • Who generates these JSON? What would be valor of the second line of the JSON that posted in the question?

  • It is a routine written in Typescript. The valor and campo are just illustrative names, because you have bank information that I can’t put in. The point is, I can’t make changes to the routine, I have to find a way to fix this JSON in the program itself.

1 answer

1


It’s really hard to answer since you didn’t say anything about as want to rewrite the JSON (you just want to rewrite it to become a valid JSON, you want to insert some data that may be missing or something else...?) nor if the points with wrong structure always follow the same pattern as your example.

With that said, I think the best solution (with the information you have made available) is to include a block try-except to treat this error as an exception. Write something like this:

try:
    ...
    content = json.loads(content)
    ...
except JSONDecodeError:
    content = 'JSON mal estruturado...'
except Exception as erro:
    content = f'JSON com erro: {erro}'

If you load multiple Jsons into your program this will cause your script to continue writing to the CSV file even if one of the Jsons shows an error. I hope it helps you, but consider providing more information so that we can answer it more accurately.

  • Sorry, I thought I was able to be specific. What I wanted was to rewrite this JSON, but ignore the error I put above, ie some way to rewrite this JSON and remove the [{}][{ appearing left and right.

  • Before you go through json.loads, type(content) == str, right? If so, have you tried using replace()? if '[{}][{' in content:
 content = content.replace('[{}][{', '')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.