Print JSON data with Key in Python

Viewed 50

Dears,

Would you be so kind to help me with my Python API. I try to export just specific properties from my online-shop. But if I do, there is no Key in front of the property value. So I tried to concette data and wrote the Key manually into my print()

Please see my code below:

wcorders = wcapi.get("orders", params={"status": "completed"}).json()

if wcorders == []:
    print("Keine neuen Bestellungen!")
    
elif wcorders != []:

    i = 0
    while i < len(wcorders):

        b = wcorders[i]["id"]
        print(("ID: "), (json.dumps(wcorders[i]["id"], indent=4, sort_keys=True)))
        print(("Number: "), (json.dumps(wcorders[i]["number"], indent=4, sort_keys=True)))
        print(("shipping"), (json.dumps(wcorders[i]["shipping"]["company"], indent=4, sort_keys=True)))
        
        data = {
            "status": "completed"
        }
        
        wcapi.put(("orders/{0}".format(b)), data).json()
        
        i = i+1
    
    print("Bestellungen wurden importiert und Status auf 'erledigt' gesetzt")

The JSON response of wcorders looks like this:

    {
  "id": 727,
  "parent_id": 0,
  "number": "727",
  "order_key": "wc_order_58d2d042d1d",
  "created_via": "rest-api",
  "version": "3.0.0",
  "status": "processing",
  "currency": "USD",
  "date_created": "2017-03-22T16:28:02",
  "date_created_gmt": "2017-03-22T19:28:02",
  "date_modified": "2017-03-22T16:28:08",
  "date_modified_gmt": "2017-03-22T19:28:08",
  "discount_total": "0.00",
  "discount_tax": "0.00",
  "shipping_total": "10.00",
  "shipping_tax": "0.00",
  "cart_tax": "1.35",
  "total": "29.35",
  "total_tax": "1.35",
  "prices_include_tax": false,
  "customer_id": 0,
  "customer_ip_address": "",
  "customer_user_agent": "",
  "customer_note": "",
  "billing": {
    "first_name": "John",
    "last_name": "Doe",
    "company": "",
    "address_1": "969 Market",
    "address_2": "",
    "city": "San Francisco",
    "state": "CA",
    "postcode": "94103",
    "country": "US",
    "email": "john.doe@example.com",
    "phone": "(555) 555-5555"
  },

When I want to print just one value, like print(json.dumps(wcorders[i]["id"], indent=4, sort_keys=True)), it only shows "727" without the Key.

Do you know a proper solution?

I am confident you can help me with it :-)

Thanks in advance and best regards

0 Answers
Related