0

I'm having trouble comparing 2 iterations from a JSON for matching asset Ids and print values of all matched assets, having multiple keys and multiple values I'm getting confused on to do this effectively.

Is there way I can compare :

assetId = key

and

asset = value["asset"]["id"]

from different iterations and if any of all assets match, then print the (item, listingId, subTotal) of all matched asset numbers ?

Not the complete JSON but the part I need to work with :

{ 
  "success": true,
  "more": false,
  "listinginfo": {
    "719811220882156383": {
      "listingid": "719811220882156383",
      "converted_price": 657,
      "converted_fee": 97,
      "asset": {
        "currency": 0,
        "appid": 730,
        "contextid": "2",
        "id": "4495490578",
        "amount": "1",
      }
    },
    "719811220882158733": {
      "listingid": "719811220882158733",
      "converted_price": 49,
      "converted_fee": 6,
      "asset": {
        "currency": 0,
        "appid": 730,
        "contextid": "2",
        "id": "2778717097",
        "amount": "1",
      }
    },
  "purchaseinfo": [    
  ],
  "assets": {
    "730": {
      "2": {
        "4495490578": {
          "currency": 0,
          "appid": 730,
          "contextid": "2",
          "id": "4495490578",
          "classid": "1389958217",
          "instanceid": "188530139",
          "amount": "1",
          "status": 2,
          "original_amount": "1",
          "background_color": "",
          "icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot7HxfDhjxszJemkV09-5lpKKqPrxN7LEmyVQ7MEpiLuSrYmnjQO3-UdsZGHyd4_Bd1RvNQ7T_FDrw-_ng5Pu75iY1zI97bhLsvQz",
          "icon_url_large": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot7HxfDhjxszJemkV09-5lpKKqPrxN7LEm1Rd6dd2j6eQ9N2t2wK3-ENsZ23wcIKRdQE2NwyD_FK_kLq9gJDu7p_KyyRr7nNw-z-DyIFJbNUz",
          "name": "AK-47 | Redline",
          "name_color": "D2D2D2",
          "type": "Classified Rifle",
          "market_name": "AK-47 | Redline (Field-Tested)",
          "market_hash_name": "AK-47 | Redline (Field-Tested)",
          "owner": 0
        },
        "2778717097": {
          "currency": 0,
          "appid": 730,
          "contextid": "2",
          "id": "2778717097",
          "classid": "720294710",
          "instanceid": "188530139",
          "amount": "1",
          "status": 2,
          "original_amount": "1",
          "background_color": "",
          "icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgposr-kLAtl7PLZTjlH7du6kb-FlvD1DLPUl31IppF3jLvC8d2i2lKx-BVpN2_3IdPDcwJqMF7Z_VS7yOy-0JbuucvKyiF9-n51aSXb4FU",
          "icon_url_large": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgposr-kLAtl7PLZTjlH7du6kb-FlvD1DLPUl31I18lwmO7Eu9jzjga3r0JrYT-mJ9CQJgA6ZA2G-1XqkOjmgpO5tZ-YnHIxv3Yh5izD30vgRbaAlsY",
          "name": "Desert Eagle | Naga",
          "name_color": "D2D2D2",
          "type": "Restricted Pistol",
          "market_name": "Desert Eagle | Naga (Battle-Scarred)",
          "market_hash_name": "Desert Eagle | Naga (Battle-Scarred)",
          "owner": 0
        },
        "4487026121": {
          "currency": 0,
          "appid": 730,
          "contextid": "2",
          "id": "4487026121",
          "classid": "1428441554",
          "instanceid": "0",
          "amount": "1",
          "status": 2,
          "original_amount": "1",
          "background_color": "",
          "icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXU5A1PIYQNqhpOSV-fRPasw8rsUFJ5KBFZv668FFMu1aPMI24auITjxteJwPXxY72AkGgIvZAniLjHpon2jlbl-kpvNjz3JJjVLFG9rl1YLQ",
          "tradable": 0,
          "name": "Operation Breakout Weapon Case",
          "name_color": "D2D2D2",
          "type": "Base Grade Container",
          "market_name": "Operation Breakout Weapon Case",
          "market_hash_name": "Operation Breakout Weapon Case",
          "owner": 0
        }
      }
    }
  }
 }
}

The Parsing I was able to do so far:

            InfoJson = json.loads(response)
            assetInfoJson = InfoJson["assets"]["730"]["2"]
            for key, value in assetInfoJson.iteritems():
                assetId = key
                try:
                    image = value["icon_url"]
                    item = value["market_hash_name"]
                except KeyError:
                  continue

            listingInfoJson = InfoJson["listinginfo"]
            for key, value in listingInfoJson.iteritems():
                listingId = key
                try:
                    subTotal = value["converted_price"]
                    feeAmount = value["converted_fee"]
                    asset = value["asset"]["id"]
                except KeyError:
                  continue
            if assetId == asset:
                print ("Found Matching asset :",assetId)
                print ("Found Matching name : %s, listingid : %s, subtotal : %s" % (item, listingId, subTotal))

Unfortunately this way doesn't compare both complete lists for matching Asset ids, only when the last assetId matches the first asset from both iterations.

1 Answer 1

1

You are using the for loops incorrectly. Since both loops are at the same level, the values of assetId, image and item will be the value assigned the last time the loop ran successfully.

A similar problem is happening with the second for loop.

Finally, your if condition will only compare the values of the last element in each list.

A solution to your problem would be to use the in operator to find the given ID in the proper dictionary

InfoJson = json.loads(response)
assetInfoJson = InfoJson["assets"]["730"]["2"]
listingInfoJson = InfoJson["listinginfo"]

for key, value in listingInfoJson.iteritems():
    listingId = key
    try:
        subTotal = value["converted_price"]
        feeAmount = value["converted_fee"]
        asset = value["asset"]["id"]
    except KeyError:
      continue

    if asset in assetInfoJson:
        # Get asset details if it's present in the assetInfoJson dict
        assetInfo = assetInfoJson[asset]
        try:
            image = assetInfo["icon_url"]
            item = assetInfo["market_hash_name"]
        except KeyError:
          continue

        print ("Found Matching asset :",assetId)
        print ("Found Matching name : %s, listingid : %s, subtotal : %s" % (item, listingId, subTotal))
Sign up to request clarification or add additional context in comments.

2 Comments

awesome, totally failed to think of it this way ! This worked great to print matching info, but I want to ask ? How can I use all matched data outside the loop ? For example, in another function outside the loop, do print (float(subTotal + feeAmount), asset) of only matched items ?
@MarieAnne, You'll need to build a list, adding the items you want to reference later, you could save IDs only or full details. The you'll use that list to print the relevant information

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.