I'm new to Python. I'm trying to write a script to take in a JSON file and return the contents of the JSON to an API. I do not use json.dumps()
because (at least in the documentation) the JSON isn't converted into a string before it gets shipped out.
import sys, json
from woocommerce import API
products_json = sys.argv[1]
wcapi = API(
url=SITE_URL,
consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET,
wp_api=True,
version="wc/v2"
)
with open(products_json, 'r') as f:
products_data = json.load(f)
print(wcapi.post("products/batch", products_data).json())
This is the contents of my JSON:
{
"create": [
{
"name": "Woo Single #1",
"type": "simple",
"status": "draft",
"regular_price": "21.99",
"virtual": true,
"downloadable": true,
"downloads": [
{
"name": "Woo Single",
"file": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_4_angle.jpg"
}
],
"categories": [
{
"id": 11
},
{
"id": 13
}
]
},
{
"name": "New Premium Quality",
"type": "simple",
"status": "draft",
"regular_price": "21.99",
"description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
"categories": [
{
"id": 9
},
{
"id": 14
}
],
}
]
}
The error message that I get is as follows:
C:\Users\absol\Desktop>python apmarble-wcapi.py "test.json"
Traceback (most recent call last):
File "apmarble-wcapi.py", line 15, in <module>
products_data = json.load(f)
File "C:\Users\absol\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\absol\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\absol\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\absol\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 355, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 40 column 9 (char 1422)
How can I import the JSON correctly so that it ships out as I am trying to do?