skip to Main Content

I am trying to retrieve the titles of multiple items that have been under a single order.
There can be multiple subsections under the ‘line_items’ where the information of each item is displayed, title being one of the parameters for every item.

[0] brings me the first item title.

[-1] brings me the last item title.

How do I interact with the subsections in between? Is there a way to return all associated order titles whether there’s 1 or more?

  r = requests.get("jsonURL", params="jsonparams")
  data = r.json()

  for item in data['orders']:
    purchased = item['line_items'][0]['title']
    purchased1 = item['line_items'][-1]['title']

3

Answers


  1. You can do this by adding an inner loop like this:

    r = requests.get("jsonURL", params="jsonparams")
    data = r.json()
    
    for items in data['orders']:
        for item in items['line_items']:
           print(item['title'])
    

    This will print out the titles of the items in each of the orders.

    If you only want to print out a subset of the items you can use a slice on the items['line_items']. If you want to print only the first n items, then you would write items['line_items'][:n]. If you want to print only the last n items, you would write items['line_items'][-n]. You can even combine the two, for instance items['line_items'][1:-2] will skip the first and last two items in the list.

    Login or Signup to reply.
  2. Hope this helps for you.

    r = requests.get("jsonURL", params="jsonparams")
    data = r.json()
    
    result = [items['items'] for items in data['orders']['line_items']]
    

    If you want to get the first index:

    print(result[0])
    

    If you want to get the first index:

    print(result[-1])
    
    Login or Signup to reply.
  3. The JSON from your code will look somewhat like this:

        data =  {
            "orders": {
            "line_items": [
                {
                    "title": "abc"
                },
                {
                    "title": "def"
                },
                {
                    "title": "ghi"
                },
                {
                    "title": "jkl"
                },
                {
                    "title": "mno"
                }
            ]
        }
    }
    

    So, in order to get titles for all items, loop through each item in the line_items list, look for the ‘title’ key and append its values to an empty list called purchased:

    purchased = []
    for item in data['orders']['line_items']:
        if 'title' in item.keys():
            purchased.append(item['title'])
    print(purchased)
    #Output:
    ['abc', 'def', 'ghi', 'jkl', 'mno']
    

    If you also want the quantity of each item purchased, you can use Counter:

    from collections import Counter
    print(Counter(purchased))
    #Output:
    Counter({'abc': 1, 'def': 1, 'ghi': 1, 'jkl': 1, 'mno': 1})
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search