Skip to content

Instantly share code, notes, and snippets.

@junlong
Forked from mbrzusto/fix_bad_quote_json.py
Created July 3, 2024 01:50
Show Gist options
  • Select an option

  • Save junlong/061c877b3a5b6bb53b5481b8bd6be1bd to your computer and use it in GitHub Desktop.

Select an option

Save junlong/061c877b3a5b6bb53b5481b8bd6be1bd to your computer and use it in GitHub Desktop.
convert single quote json data file to double quote json data file (without mangling inner quotes)
__author__ = 'mbrzustowicz'
# metadata.json has single quotes like this
# {'asin': 'B00M0AEPXG', 'imUrl': 'http://ecx.images-amazon.com/images/I/51hcXTUeHLL._BO2,204,203,200_ ..... }
# so the strategy is to read each line as a string, and dump into a REAL json file
import json
import ast
fr=open("/Users/mbrzustowicz/Downloads/metadata.json")
fw=open("/Users/mbrzustowicz/amazon_product_metadata.json", "w")
for line in fr:
json_dat = json.dumps(ast.literal_eval(line))
dict_dat = json.loads(json_dat)
json.dump(dict_dat, fw)
fw.write("\n")
fw.close()
fr.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment