Search code examples
pythonjsonpython-3.xpython-requestsinventory

How to filter json response with steam api python


Hello I would like to fetch and filter my inventory using steam api. To do that I am using python requests library. I am able to read the response but I also want my code to filter it so it will only show the the 'market_name' of the items listed in my inventory. Here's my code

import requests
import pprint

payload = {
    'l': 'english',
    'count': '5',
}

r = requests.get('https://steamcommunity.com/inventory/76561198231636540/440/2', params=payload)

pprint.pprint(r.json)

and the part of my response

{'assets': [{'amount': '1',
             'appid': 440,
             'assetid': '8336159998',
             'classid': '2650378220',
             'contextid': '2',
             'instanceid': '3630556994'},
            {'amount': '1',
             'appid': 440,
             'assetid': '8718313756',
             'classid': '1336074342',
             'contextid': '2',
             'instanceid': '11041153'},
            {'amount': '1',
             'appid': 440,
             'assetid': '8621757160',
             'classid': '1336074104',
             'contextid': '2',
             'instanceid': '11050161'},
            {'amount': '1',
             'appid': 440,
             'assetid': '8336123257',
             'classid': '1336074005',
             'contextid': '2',
             'instanceid': '331432'},
            {'amount': '1',
             'appid': 440,
             'assetid': '8238635150',
             'classid': '2019751157',
             'contextid': '2',
             'instanceid': '1366049788'}],
 'descriptions': [{'actions': [{'link': 'http://wiki.teamfortress.com/scripts/itemredirect.php?id=327&lang=en_US',
                                'name': 'Item Wiki Page...'},
                               {'link': 'steam://rungame/440/76561202255233023/+tf_econ_item_preview%20S%owner_steamid%A%assetid%D9271970645775411665',
                                'name': 'Inspect in Game...'}],
                   'appid': 440,
                   'background_color': '3C352E',
                   'classid': '2650378220',
                   'commodity': 0,
                   'currency': 0,
                   'descriptions': [{'color': '8847ff',
                                     'value': 'Commando Grade Sword (Factory '
                                              'New)'},
                                    {'value': 'When weapon is active:'},
                                    {'value': 'This Weapon has a large melee '
                                              'range and\n'
                                              'deploys and holsters slower'},
                                    {'color': '7ea9d1',
                                     'value': '0.5 sec increase in charge '
                                              'duration'},
                                    {'color': '7ea9d1',
                                     'value': 'Melee kills refill 25% of your '
                                              'charge meter'},
                                    {'color': 'd83636',
                                     'value': 'No random critical hits'},
                                    {'color': 'd83636',
                                     'value': '15% damage vulnerability on '
                                              'wearer'},
                                    {'value': ' '},
                                    {'value': ' '},
                                    {'value': 'Mann Co. Events Collection'},
                                    {'color': '8847ff',
                                     'value': '✔ Smissmas Sweater War Paint'},
                                    {'value': ' '},
                                    {'value': '( Not Tradable or Marketable '
                                              ')'}],
                   'icon_url': 'fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEMaQkUTxr2vTx8mMnvA-aHAfQ_ktk664MayTdinxVwPffmaWZYexDHDPMMC8ot8Qn-WGk2scE1AYO1r7hffVjqttaUM7MtMIkYHMLZD6OOMFv96kpqh6QMLJOKvmqxitamdBjm',
                   'icon_url_large': 'fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEMaQkUTxr2vTx8mMnvA-aHAfQ_ktk664MayTdinxVwPffmaWZYexDHDPMMC8ot8Qn-WGk2scE1AYO1r7hffVjqttaUM7MtMIkYHMLZD6OOMFv96kpqh6QMLJOKvmqxitamdBjm',
                   'instanceid': '3630556994',
                   'market_actions': [{'link': 'steam://rungame/440/76561202255233023/+tf_econ_item_preview%20M%listingid%A%assetid%D9271970645775411665',
                                       'name': 'Inspect in Game...'}],
                   'market_hash_name': 'Smissmas Sweater Claidheamh Mòr '
                                       '(Factory New)',
                   'market_marketable_restriction': 0,
                   'market_name': 'Smissmas Sweater Claidheamh Mòr (Factory '
                                  'New)',
                   'market_tradable_restriction': 7,
                   'marketable': 0,
                   'name': 'Smissmas Sweater Claidheamh Mòr',
                   'name_color': 'FAFAFA',
                   'tags': [{'category': 'Quality',
                             'color': 'FAFAFA',
                             'internal_name': 'paintkitweapon',
                             'localized_category_name': 'Quality',
                             'localized_tag_name': 'Decorated Weapon'},
                            {'category': 'Type',
                             'internal_name': 'melee',
                             'localized_category_name': 'Type',
                             'localized_tag_name': 'Melee weapon'},
                            {'category': 'Class',
                             'internal_name': 'Demoman',
                             'localized_category_name': 'Class',
                             'localized_tag_name': 'Demoman'},
                            {'category': 'Exterior',
                             'internal_name': 'TFUI_InvTooltip_FactoryNew',
                             'localized_category_name': 'Exterior',
                             'localized_tag_name': 'Factory New'}],
                   'tradable': 0,
                   'type': ''},
                  {'actions': [{'link': 'http://wiki.teamfortress.com/scripts/itemredirect.php?id=200&lang=en_US',
                                'name': 'Item Wiki Page...'},
                               {'link': 'steam://rungame/440/76561202255233023/+tf_econ_item_preview%20S%owner_steamid%A%assetid%D4767637082452267116',
                                'name': 'Inspect in Game...'}],
                   'appid': 440,
                   'background_color': '3C352E',
                   'classid': '1336074342',
                   'commodity': 0,
                   'currency': 0,
                   'icon_url': 'fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEIUxQSXA_2vSpEncbZAfOeD-VOzotl5MkEgWc7yVkjZLbmMWEzJAaaWfgHXaU_8F65WXVmsJ80UNGz8_UWJ1tD85_Guw',
                   'icon_url_large': 'fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEIUxQSXA_2vSpEncbZAfOeD-VOzotl5MkEgWc7yVkjZLbmMWEzJAaaWfgHXaU_8F65WXVmsJ80UNGz8_UWJ1tD85_Guw',
                   'instanceid': '11041153',
                   'market_actions': [{'link': 'steam://rungame/440/76561202255233023/+tf_econ_item_preview%20M%listingid%A%assetid%D4767637082452267116',
                                       'name': 'Inspect in Game...'}],
                   'market_hash_name': 'Strange Scattergun',
                   'market_marketable_restriction': 0,
                   'market_name': 'Strange Scattergun',
                   'market_tradable_restriction': 7,
                   'marketable': 1,
                   'name': 'Strange Scattergun',
                   'name_color': 'CF6A32',
                   'tags': [{'category': 'Quality',
                             'color': 'CF6A32',
                             'internal_name': 'strange',
                             'localized_category_name': 'Quality',
                             'localized_tag_name': 'Strange'},
                            {'category': 'Type',
                             'internal_name': 'primary',
                             'localized_category_name': 'Type',
                             'localized_tag_name': 'Primary weapon'},
                            {'category': 'Class',
                             'internal_name': 'Scout',
                             'localized_category_name': 'Class',
                             'localized_tag_name': 'Scout'}],
                   'tradable': 1,
                   'type': 'Strange Scattergun - Kills: 91'},
                  {'actions': [{'link': 'http://wiki.teamfortress.com/scripts/itemredirect.php?id=416&lang=en_US',
                                'name': 'Item Wiki Page...'}],
                   'appid': 440,
                   'background_color': '3C352E',
                   'classid': '1336074104',
                   'commodity': 0,
                   'currency': 0,
                   'descriptions': [{'color': '7ea9d1',
                                     'value': 'Deals crits while the wielder '
                                              'is rocket jumping'},
                                    {'color': 'd83636',
                                     'value': '20% slower firing speed'},
                                    {'color': 'd83636',
                                     'value': 'No random critical hits'},
                                    {'value': ' '},
                                    {'color': 'e1e10f',
                                     'value': 'The Airborne Armaments'},
                                    {'value': ' '},
                                    {'color': '8b8989',
                                     'value': 'The Liberty Launcher'},
                                    {'color': '8b8989',
                                     'value': 'The Reserve Shooter'},
                                    {'color': '8b8989',
                                     'value': 'The Market Gardener'},
                                    {'color': '8b8989',
                                     'value': "Jumper's Jeepcap"}],
                   'icon_url': 'fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEIUwoQTxDnrAdEidriCPyJGt8Mmsgy4N4DimdpxVUuZuGzM2JkJwHHVqENCPQ4oVq6WXNlsJMwUdXjob0CcF7tqsKYZHE9EdYU',
                   'icon_url_large': 'fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEIUwoQTxDnrAdEidriCPyJGt8Mmsgy4N4DimdpxVUuZuGzM2JkJwHHVqENCPQ4oVq6WXNlsJMwUdXjob0CcF7tqsKYZHE9EdYU',
                   'instanceid': '11050161',
                   'market_hash_name': 'Strange Market Gardener',
                   'market_marketable_restriction': 0,
                   'market_name': 'Strange Market Gardener',
                   'market_tradable_restriction': 7,
                   'marketable': 1,
                   'name': 'Strange Market Gardener',
                   'name_color': 'CF6A32',
                   'tags': [{'category': 'Quality',
                             'color': 'CF6A32',
                             'internal_name': 'strange',
                             'localized_category_name': 'Quality',
                             'localized_tag_name': 'Strange'},
                            {'category': 'Type',
                             'internal_name': 'melee',
                             'localized_category_name': 'Type',
                             'localized_tag_name': 'Melee weapon'},
                            {'category': 'Class',
                             'internal_name': 'Soldier',
                             'localized_category_name': 'Class',
                             'localized_tag_name': 'Soldier'}],
                   'tradable': 1,
                   'type': 'Strange Shovel - Kills: 178'},
                  {'actions': [{'link': 'http://wiki.teamfortress.com/scripts/itemredirect.php?id=655&lang=en_US',
                                'name': 'Item Wiki Page...'},
                               {'link': 'steam://rungame/440/76561202255233023/+tf_econ_item_preview%20S%owner_steamid%A%assetid%D16900367402742495547',
                                'name': 'Inspect in Game...'}],
                   'appid': 440,
                   'background_color': '3C352E',
                   'classid': '1336074005',
                   'commodity': 0,
                   'currency': 0,
                   'descriptions': [{'value': 'The more gifts you give away, '
                                              'the bigger your heart will get! '
                                              'Get what gift-givers call "the '
                                              'Smissmas spirit," and what '
                                              'cardiologists call hypertrophic '
                                              'cardiomyopathy.'},
                                    {'value': ' '},
                                    {'value': '( Not Tradable or Marketable '
                                              ')'}],
                   'icon_url': 'fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgETYRQuXBfuuzRCm9vZCvuKHOIBn90w2pxUyzFu3wJ4Yua0NjIyKlDEUfkNXqBroA7qDXEz7p9iBtHioesHLwnpsIvEZ7QycIYbTv20z5o',
                   'icon_url_large': 'fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgETYRQuXBfuuzRCm9vZCvuKHOIBn90w2pxUyzFu3wJ4Yua0NjIyKlDEUfkNXqBroA7qDXEz7p9iBtHioesHLwnpsIvEZ7QycIYbTv20z5o',
                   'instanceid': '331432',
                   'market_actions': [{'link': 'steam://rungame/440/76561202255233023/+tf_econ_item_preview%20M%listingid%A%assetid%D16900367402742495547',
                                       'name': 'Inspect in Game...'}],
                   'market_hash_name': 'Strange Spirit Of Giving',
                   'market_marketable_restriction': 0,
                   'market_name': 'Strange Spirit Of Giving',
                   'market_tradable_restriction': 7,
                   'marketable': 0,
                   'name': 'The Spirit Of Giving',
                   'name_color': 'CF6A32',
                   'tags': [{'category': 'Quality',
                             'color': 'CF6A32',
                             'internal_name': 'strange',
                             'localized_category_name': 'Quality',
                             'localized_tag_name': 'Strange'},
                            {'category': 'Type',
                             'internal_name': 'misc',
                             'localized_category_name': 'Type',
                             'localized_tag_name': 'Cosmetic'},
                            {'category': 'Class',
                             'internal_name': 'Scout',
                             'localized_category_name': 'Class',
                             'localized_tag_name': 'Scout'},
                            {'category': 'Class',
                             'internal_name': 'Sniper',
                             'localized_category_name': 'Class',
                             'localized_tag_name': 'Sniper'},
                            {'category': 'Class',
                             'internal_name': 'Soldier',
                             'localized_category_name': 'Class',
                             'localized_tag_name': 'Soldier'},
                            {'category': 'Class',
                             'internal_name': 'Demoman',
                             'localized_category_name': 'Class',
                             'localized_tag_name': 'Demoman'},
                            {'category': 'Class',
                             'internal_name': 'Medic',
                             'localized_category_name': 'Class',
                             'localized_tag_name': 'Medic'},
                            {'category': 'Class',
                             'internal_name': 'Heavy',
                             'localized_category_name': 'Class',
                             'localized_tag_name': 'Heavy'},
                            {'category': 'Class',
                             'internal_name': 'Pyro',
                             'localized_category_name': 'Class',
                             'localized_tag_name': 'Pyro'},
                            {'category': 'Class',
                             'internal_name': 'Spy',
                             'localized_category_name': 'Class',
                             'localized_tag_name': 'Spy'},
                            {'category': 'Class',
                             'internal_name': 'Engineer',
                             'localized_category_name': 'Class',
                             'localized_tag_name': 'Engineer'}],
                   'tradable': 0,
                   'type': 'The Badge - Gifts Given: 1'},
                  {'actions': [{'link': 'http://wiki.teamfortress.com/scripts/itemredirect.php?id=30195&lang=en_US',
                                'name': 'Item Wiki Page...'},
                               {'link': 'steam://rungame/440/76561202255233023/+tf_econ_item_preview%20S%owner_steamid%A%assetid%D13930633427653330675',
                                'name': 'Inspect in Game...'}],
                   'appid': 440,
                   'background_color': '3C352E',
                   'classid': '2019751157',
                   'commodity': 0,
                   'currency': 0,
                   'descriptions': [{'color': 'd83636',
                                     'value': 'Holiday Restriction: Halloween '
                                              '/ Full Moon'},
                                    {'value': ''},
                                    {'value': ' '},
                                    {'value': '( Not Tradable or Marketable '
                                              ')'}],
                   'icon_url': 'fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEDe1VBDEjdvSxLjdrjDP6zAO8Pn-U55IJS3HhulFQjYufkaWZjJ1zAUfIGWvBprAy8CCM0uMU6DNLipuhSeAjosNeSLfByZ_A-Mofd',
                   'icon_url_large': 'fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEDe1VBDEjdvSxLjdrjDP6zAO8Pn-U55IJS3HhulFQjYufkaWZjJ1zAUfIGWvBprAy8CCM0uMU6DNLipuhSeAjosNeSLfByZ_A-Mofd',
                   'instanceid': '1366049788',
                   'market_actions': [{'link': 'steam://rungame/440/76561202255233023/+tf_econ_item_preview%20M%listingid%A%assetid%D13930633427653330675',
                                       'name': 'Inspect in Game...'}],
                   'market_hash_name': 'Haunted Ethereal Hood',
                   'market_marketable_restriction': 0,
                   'market_name': 'Haunted Ethereal Hood',
                   'market_tradable_restriction': 7,
                   'marketable': 0,
                   'name': 'Haunted Ethereal Hood',
                   'name_color': '38f3ab',
                   'tags': [{'category': 'Quality',
                             'color': '38f3ab',
                             'internal_name': 'haunted',
                             'localized_category_name': 'Quality',
                             'localized_tag_name': 'Haunted'},
                            {'category': 'Type',
                             'internal_name': 'misc',
                             'localized_category_name': 'Type',
                             'localized_tag_name': 'Cosmetic'},
                            {'category': 'Class',
                             'internal_name': 'Demoman',
                             'localized_category_name': 'Class',
                             'localized_tag_name': 'Demoman'},
                            {'category': 'Class',
                             'internal_name': 'Spy',
                             'localized_category_name': 'Class',
                             'localized_tag_name': 'Spy'}],
                   'tradable': 0,
                   'type': 'Level 10 Costume Piece'}],
 'last_assetid': '8238635150',
 'more_items': 1,
 'rwgrsn': -2,
 'success': 1,
 'total_inventory_count': 246}

Is there a way to make my code only show the 'market_name' of all items listed in the response ? Also I would like to ask from where can I learn more about making http requests and json data so I don't have to post a question here every time I have a problem that a youtube tutorial doesn't cover.

Any help would be appreciated.


Solution

  • requests.Response.json() uses the built-in JSON module method json.loads() for JSON deserialization, so you can use an object hook to print out that specific value if the key is found.

    import requests
    
    
    def hook(dct):
        for k, v in dct.items():
            if k == "market_name":
                print(v)
    
    
    r = requests.get(
        "https://steamcommunity.com/inventory/76561198231636540/440/2",
        params={"l": "english", "count": "5",},
    )
    
    r.json(object_hook=hook)
    

    Output

    Smissmas Sweater Claidheamh Mòr (Factory New)
    Strange Scattergun
    Strange Market Gardener
    Strange Spirit Of Giving
    Haunted Ethereal Hood