Search code examples
pythonjsonroblox

Json error coming while using roblox api in python


I was just playing with roblox API trying to create something that can buy a item from roblox catalog but I encountered a strange error and would like your support

import re
import json
from requests import post, get

cookie = "somethingicantshow"
ID = 1562150

# getting x-csrf-token

token = post("https://auth.roblox.com/v2/logout", cookies={".ROBLOSECURITY": cookie}).headers['X-CSRF-TOKEN']
print(token)
# getting item details

detail_res = get("https://www.roblox.com/library/" + str(ID))
text = detail_res.text

info = post("https://catalog.roblox.com/v1/catalog/items/details",
                           json={"items": [{"itemType": "Asset", "id": int(ID)}]},
                           headers={"x-csrf-token": token}, cookies={".ROBLOSECURITY": cookie}).json()["data"][0]

productId = post("https://apis.roblox.com/marketplace-items/v1/items/details",
                   json={"itemIds": [info.get("collectibleItemId")]},
                   headers={"x-csrf-token": token}, cookies={".ROBLOSECURITY": cookie})  #int(get("https://api.roblox.com/marketplace/productinfo?assetId="+ str(ID)).json("ProductId"))#["ProductId"])
productId = productId.json()[0]["collectibleProductId"]
expectedPrice = 0 #int(re.search("data-expected-price=\"(\d+)\"", text).group(1))
expectedSellerId = 1 #int(re.search("data-expected-seller-id=\"(\d+)\"", text).group(1))

headers = {
    "x-csrf-token": token,
    "content-type": "application/json; charset=UTF-8"
}

data = {
    "expectedCurrency": 1,
    "expectedPrice": expectedPrice,
    "expectedSellerId": expectedSellerId
}

dataLoad = json.dumps(data)
buyres = post("https://economy.roblox.com/v1/purchases/products/" + str(productId), headers=headers,
              data=dataLoad,
              cookies={".ROBLOSECURITY": cookie})

if buyres.status_code == 200:
    print("Successfully bought item")
else:
    print("failed" + str(buyres.status_code))

The error was :

Traceback (most recent call last):
  File "C:\Users\Aarush Kumar\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\models.py", line 971, in json
    return complexjson.loads(self.text, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aarush Kumar\AppData\Local\Programs\Python\Python311\Lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aarush Kumar\AppData\Local\Programs\Python\Python311\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aarush Kumar\AppData\Local\Programs\Python\Python311\Lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Aarush Kumar\Downloads\limitedsnipe\limitedSniper.py", line 24, in <module>
    productId = productId.json()[0]["collectibleProductId"]
                ^^^^^^^^^^^^^^^^
  File "C:\Users\Aarush Kumar\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\models.py", line 975, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Would really appreciate any help.

I tried a lot of stuff and read a lot of topics over on the devforum but since this is a Python error I had to take help from here.


Solution

  • Json is a data format that is readable for humans. It basically encodes dictionaries, which are just bytes in memory, to a long string, for storage. It also allows decoding a string to a dictionary object in memory.

    You got a JSONDecodeError, so I'd guess the json string you get from the roblox api is not valid json.

    You can investigate that by printing out the what you got from the roblox api and see if it looks like json data. There are online json validators as well.