Home > Software design >  I need return JSON as beauty table style
I need return JSON as beauty table style

Time:10-28

I have this code and works well


import requests 
import xml.etree.ElementTree as ET
import json

def get_stock(sku):
    params = {'ItemId': sku}
    base_url = 'http://10.0.0.25/api/GetSku'
    response = requests.get(base_url, params=params)
    json_parsed = json.loads(response.content)
     
    if response.status_code == 200:
      return [item for item in response.json() if item['Site'] == "12"]
    else:
      return None

def jprint(obj):
    text = json.dumps(obj, sort_keys=True, indent=4)
    print(text)
jprint(get_stock(101025HRLONDON))

The output is json:

[
   {
        "ItemId": "101025HRLONDON",
        "Site": "12",
        "Warehouse": "17",
        "availablePhysical": 1.0
    },
    {
        "ItemId": "101025HRLONDON",
        "Site": "12",
        "Warehouse": "33",
        "availablePhysical": 1.0
    },
    {
        "ItemId": "101025HRLONDON",
        "Site": "12",
        "Warehouse": "34",
        "availablePhysical": 1.0
    },
]

I need a way to convert the above output to beauty table or similar like this:

 ---- ------ ---------------------- 
| WH |aval. | ItemID               |
 ---- ------ ---------------------- 
| 17 |    1 | 101025HRLONDON       |
| 33 |    1 | 101025HRLONDON       |
| 34 |    1 | 101025HRLONDON       |
 ---- ------ ---------------------- 

Please share your experience working with something like this.

Thank you

CodePudding user response:

There are both PrettyTable and BeautifulTable. You can pip install these and read the docs. If you really want to make something useful, OpenpyXl is the way to go.

  • Related