Hello,
For one of my work related api's i am getting a JSON response back from a API call that i make. In this JSON response all product info is located and i need to save parts of it in a database.
The problem that i have is that i can get the data to a CSV but because for 1 of the fields is also an array this is not displayed correctly.
The JSON response is as follows (shortened down quite alot):
{
"success": true,
"data": [
{
"idproduct": 12609836,
"idvatgroup": 10059,
"idsupplier": 42582,
"productcode": "00000A00186918",
"name": "A00186918 - 00000A00186918 - Partytent 3x3 met 2 Zijwanden Grijs",
"price": 0,
"fixedstockprice": 0,
"productcode_supplier": "0",
"deliverytime": 99,
"description": "Partytent 3x3 met 2 Zijwanden Grijs",
"barcode": "",
"unlimitedstock": false,
"assembled": false,
"type": "normal",
"weight": 0,
"length": 146,
"width": 20,
"height": 20,
"minimum_purchase_quantity": 0,
"purchase_in_quantities_of": 0,
"hs_code": "",
"country_of_origin": "",
"active": true,
"comment_count": 0,
"analysis_abc_classification": null,
"analysis_pick_amount_per_day": null,
"tags": [],
"productfields": [],
"images": [],
"stock": [
{
"idwarehouse": 4861,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
},
{
"idwarehouse": 4863,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
},
{
"idwarehouse": 4862,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
},
{
"idwarehouse": 4865,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
},
{
"idwarehouse": 4870,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
},
{
"idwarehouse": 4871,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
},
{
"idwarehouse": 4872,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
},
{
"idwarehouse": 4873,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
},
{
"idwarehouse": 5397,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
}
],
"pricelists": []
},
{
"idproduct": 12609870,
"idvatgroup": 10059,
"idsupplier": 42582,
"productcode": "00000A00189619",
"name": "A00189619 - 00000A00189619 - Partytent 3x3 met 4 Zijwanden Grijs",
"price": 0,
"fixedstockprice": 0,
"productcode_supplier": "0",
"deliverytime": 99,
"description": "Partytent 3x3 met 4 Zijwanden Grijs",
"barcode": "",
"unlimitedstock": false,
"assembled": false,
"type": "normal",
"weight": 0,
"length": 146,
"width": 20,
"height": 20,
"minimum_purchase_quantity": 0,
"purchase_in_quantities_of": 0,
"hs_code": "",
"country_of_origin": "",
"active": true,
"comment_count": 0,
"analysis_abc_classification": null,
"analysis_pick_amount_per_day": null,
"tags": [],
"productfields": [],
"images": [],
"stock": [
{
"idwarehouse": 4861,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
},
{
"idwarehouse": 4863,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
},
{
"idwarehouse": 4862,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
},
{
"idwarehouse": 4865,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
},
{
"idwarehouse": 4870,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
},
{
"idwarehouse": 4871,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
},
{
"idwarehouse": 4872,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
},
{
"idwarehouse": 4873,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
},
{
"idwarehouse": 5397,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
}
],
"pricelists": []
},
{
"idproduct": 10791362,
"idvatgroup": 9520,
"idsupplier": 42582,
"productcode": "V0718037867724",
"name": "WD - Western Digital My Passport Go Portable SSD Drive, 1TB, USB 3.0, cobalt blue",
"price": 0,
"fixedstockprice": 0,
"productcode_supplier": "-NA-",
"deliverytime": 99,
"description": null,
"barcode": "0718037867724",
"unlimitedstock": false,
"assembled": false,
"type": "normal",
"weight": null,
"length": null,
"width": null,
"height": null,
"minimum_purchase_quantity": 0,
"purchase_in_quantities_of": 0,
"hs_code": null,
"country_of_origin": null,
"active": true,
"comment_count": 0,
"analysis_abc_classification": null,
"analysis_pick_amount_per_day": null,
"tags": [],
"productfields": [],
"images": [],
"stock": [
{
"idwarehouse": 4871,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
},
{
"idwarehouse": 4873,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
},
{
"idwarehouse": 4865,
"stock": 0,
"reserved": 0,
"reservedbackorders": 0,
"reservedpicklists": 0,
"reservedallocations": 0,
"freestock": 0
}
],
"pricelists": []
}
]
}
Currently i have this working by taking this JSON output and transform it to XML and then use xmlstarlet to grab the items that i want. The problem with this, is that it takes quite a long time before the complete thing is processed so i want to rebuild it to python in the hopes that it will be alot faster (apart from getting the data itself, i cannot speed that up).
The code that i currently have is as follows:
import json
content = '{"success":true,"data":[{"idproduct":12609836,"idvatgroup":10059,"idsupplier":42582,"productcode":"00000P00189918","name":"P00189918 - 8719689225469 - Garden Royal Easy up Partytent 3x3 met 2 Zijwanden Donker Grijs","price":0,"fixedstockprice":0,"productcode_supplier":"0","deliverytime":99,"description":"Garden Royal Easy up Partytent 3x3 met 2 Zijwanden Donker Grijs","barcode":"","unlimitedstock":false,"assembled":false,"type":"normal","weight":0,"length":146,"width":20,"height":20,"minimum_purchase_quantity":0,"purchase_in_quantities_of":0,"hs_code":"","country_of_origin":"","active":true,"comment_count":0,"analysis_abc_classification":null,"analysis_pick_amount_per_day":null,"tags":[],"productfields":[],"images":[],"stock":[{"idwarehouse":4861,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0},{"idwarehouse":4863,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0},{"idwarehouse":4862,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0},{"idwarehouse":4865,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0},{"idwarehouse":4870,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0},{"idwarehouse":4871,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0},{"idwarehouse":4872,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0},{"idwarehouse":4873,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0},{"idwarehouse":5397,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0}],"pricelists":[]},{"idproduct":12609870,"idvatgroup":10059,"idsupplier":42582,"productcode":"00000P00189919","name":"P00189919 - 8719689225476 - Garden Royal Easy up Partytent 3x3 met 4 Zijwanden Donker Grijs","price":0,"fixedstockprice":0,"productcode_supplier":"0","deliverytime":99,"description":"Garden Royal Easy up Partytent 3x3 met 4 Zijwanden Donker Grijs","barcode":"","unlimitedstock":false,"assembled":false,"type":"normal","weight":0,"length":146,"width":20,"height":20,"minimum_purchase_quantity":0,"purchase_in_quantities_of":0,"hs_code":"","country_of_origin":"","active":true,"comment_count":0,"analysis_abc_classification":null,"analysis_pick_amount_per_day":null,"tags":[],"productfields":[],"images":[],"stock":[{"idwarehouse":4861,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0},{"idwarehouse":4863,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0},{"idwarehouse":4862,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0},{"idwarehouse":4865,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0},{"idwarehouse":4870,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0},{"idwarehouse":4871,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0},{"idwarehouse":4872,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0},{"idwarehouse":4873,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0},{"idwarehouse":5397,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0}],"pricelists":[]},{"idproduct":10791362,"idvatgroup":9520,"idsupplier":42582,"productcode":"E0718037867724","name":"WD - Western Digital My Passport Go Portable SSD Drive, 1TB, USB 3.0, cobalt blue","price":0,"fixedstockprice":0,"productcode_supplier":"-NA-","deliverytime":99,"description":null,"barcode":"0718037867724","unlimitedstock":false,"assembled":false,"type":"normal","weight":null,"length":null,"width":null,"height":null,"minimum_purchase_quantity":0,"purchase_in_quantities_of":0,"hs_code":null,"country_of_origin":null,"active":true,"comment_count":0,"analysis_abc_classification":null,"analysis_pick_amount_per_day":null,"tags":[],"productfields":[],"images":[],"stock":[{"idwarehouse":4871,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0},{"idwarehouse":4873,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0},{"idwarehouse":4865,"stock":0,"reserved":0,"reservedbackorders":0,"reservedpicklists":0,"reservedallocations":0,"freestock":0}],"pricelists":[]}]}'
products = json.loads(content)
products = products["data"]
for product in products:
print(product["idproduct"],";",product["productcode"],product["name"],product["stock"][1]["idwarehouse"])
The problem however is that with the 3rd product, the warehouse where it grabs the data from it not the same as the other products. This will create faulty results.
I tried different things but i cannot seem to figure out how i get the results as below:
12609836;10059;42582;00000A00186918;A00186918 - 00000A00186918 - Partytent 3x3 met 2 Zijwanden Grijs;0;0;0;99;;normal;0;0;0;;0;0;0;0;0;0;0;0;
12609870;10059;42582;00000A00189619;A00189619 - 00000A00189619 - Partytent 3x3 met 4 Zijwanden Grijs;0;0;0;99;;normal;0;0;0;;0;0;0;0;0;0;0;0;
10791362;9520;42582;V0718037867724;WD - Western Digital My Passport Go Portable SSD Drive, 1TB, USB 3.0, cobalt blue;0;0;-NA-;99;0718037867724;normal;;;;;0;;;;0;;0;;
Since the above results arent very clear i am posting the xmlstarlet code that i use to clearify:
xmlstarlet sel -T -t -m /searchresults/data -v "concat(idproduct,';', \
idvatgroup,';', \
idsupplier,';', \
productcode,';', \
name,';', \
price,';', \
fixedstockprice,';', \
productcode_supplier,';', \
deliverytime,';', \
barcode,';', \
type,';', \
stock[idwarehouse="4861"]/freestock,';', \
stock[idwarehouse="4861"]/stock,';', \
stock[idwarehouse="4861"]/reserved,';', \
stock[idwarehouse="4861"]/backorder,';', \
stock[idwarehouse="4865"]/freestock,';', \
stock[idwarehouse="4862"]/freestock,';', \
stock[idwarehouse="4863"]/freestock,';', \
stock[idwarehouse="4870"]/freestock,';', \
stock[idwarehouse="4871"]/freestock,';', \
stock[idwarehouse="4872"]/freestock,';', \
stock[idwarehouse="4873"]/freestock,';', \
stock[idwarehouse="5397"]/freestock,';', \
stock[idwarehouse="6546"]/freestock
)"
Who can tell me how i can solve this issue? or point me in the right direction.