Skip to content

Instantly share code, notes, and snippets.

@mbrzusto
Created November 9, 2015 20:21
Show Gist options
  • Save mbrzusto/23fe728966247f25f3ec to your computer and use it in GitHub Desktop.
Save mbrzusto/23fe728966247f25f3ec 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()
@juliechix
Copy link

Thank you so much. I am working with Amazon product metadata (looks like you were too) and this worked great!

@Jz1116
Copy link

Jz1116 commented Oct 21, 2022

Thank you so much!

@gabr1elt-arc
Copy link

Cool, Txs.

@juliulli
Copy link

This should be a json module feature. That´s so easy 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment