Python Api parsing Json problem | API Development | TORN

Python Api parsing Json problem

    • PrecatorBaxter [3288988]
    • Role: Civilian
    • Level: 18
    • Posts: 29
    • Karma: 3
    • Last Action: 10 months
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Thread created on 19:25:26 - 15/05/24 (11 months ago)
    |
    Last replied 10:39:48 - 16/05/24 (11 months ago)
    I am using this endpoint > https://api.torn.com/torn/?selections=items&key=

    I see all the items in the browser, but I am having problems.

    response = requests.get("https://api.torn.com/torn/?selections=items&key=" + key)
    json_data = json.dumps(response.text)


    After this I am having a problem. I want to loop over each item and loop over the results and print the market_value variable. I know I am supposed to do like for x in json_data but it seems to get stuck on the top level items dict i believe. Sorry I am new to Python and just trying to figure this out.
    • pobk [3171827]
    • Role: Civilian
    • Level: 57
    • Posts: 154
    • Karma: 171
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 20:33:51 - 15/05/24 (11 months ago)
    Post link copied to clipboard Copy post link
    If you're using requests, you can use response.json()

    That will return a python object you can iterate over.
    Last edited by pobk on 20:34:27 - 15/05/24 (11 months ago)

    ~ P

    Support my suggestions: Faction API Keys

    • tiksan [2383326]
    • Role: Civilian
    • Level: 100
    • Posts: 992
    • Karma: 1,313
    • Last Action: 2 hours
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 04:06:00 - 16/05/24 (11 months ago)
    Post link copied to clipboard Copy post link
    You should check what the value in json_data is (using print or a debugger) since the API response has a with an "items" key. You can use https://tornapi.tornplayground.eu/torn/items to see the full response format. So you'd have to use something like..



    I wish Ched had code formatting in forums...

    https://cdn.discordapp.com/attachments/719011659561173012/1237732490513481809/Sith_Larping_Asshats.gif?ex=678f8935&is=678e37b5&hm=6d9a2ed93994782d9020538e0ec71a3d072964d584aceb09e9e6f547752d41e0&

    • PrecatorBaxter [3288988]
    • Role: Civilian
    • Level: 18
    • Posts: 29
    • Karma: 3
    • Last Action: 10 months
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 10:39:48 - 16/05/24 (11 months ago)
    Post link copied to clipboard Copy post link
    Thanks alot this worked!
Reply
Thread Title: