skip to Main Content

So I needed help because my json is wrong and I need to fix it with the code itself.

The JSON is :

{"3704969059":{"Lunar Moth Headwings",100,1680750150,500,"https://tr.rbxcdn.com/5c0a02da770746e4ac9a1760d427f716/150/150/Hat/Png"},"13114264635":{"Cow Beret",70,1682603172,200,"https://tr.rbxcdn.com/7d39a32f04f00844113f4b3ab355517a/150/150/Hat/Png"},"13160317604":{"Insignia Helmet",2500,1682593511,25,"https://tr.rbxcdn.com/55feabd27486feb51eb4f0db2209fecf/150/150/Hat/Png"}}

The error is json.decoder.JSONDecodeError: Expecting ':' delimiter: line 1 column 38 (char 37)
I found the problem and it is that there are numbers and they are not enclosed with double quotes I want to add those double quotes but I am kinda stuck.

I tried searching but didn’t find and relative solution. I just want to make the json proper

3

Answers


  1. Your problem is that the objects which are the values of your top level JSON object do not have field names.

    Either change your level two literals into lists (replace the curly brackets with squares)…

    {
        "3704969059": [
            "Lunar Moth Headwings",
            // etc...
            "https://tr.rbxcdn.com/5c0a02da770746e4ac9a1760d427f716/150/150/Hat/Png"
        ]
        //etc...
    }
    

    … or add relevant field names to the object literal, as below:

    {
        "3704969059": {
            "name": "Lunar Moth Headwings",
            // etc...
            "url": "https://tr.rbxcdn.com/5c0a02da770746e4ac9a1760d427f716/150/150/Hat/Png"
        }
        //etc...
    }
    

    This is a better option as it adds context to your data

    Login or Signup to reply.
  2. The data you have is ambiguous as described in an other answer by CJC. It may be a better solution to modify the original scraping code to label this information better. Here is a shorter term solution to your answer that forces the data to read as a dictionary of lists.

    js = '''{"3704969059":{"Lunar Moth Headwings",100,1680750150,500,"https://tr.rbxcdn.com/5c0a02da770746e4ac9a1760d427f716/150/150/Hat/Png"},"13114264635":{"Cow Beret",70,1682603172,200,"https://tr.rbxcdn.com/7d39a32f04f00844113f4b3ab355517a/150/150/Hat/Png"},"13160317604":{"Insignia Helmet",2500,1682593511,25,"https://tr.rbxcdn.com/55feabd27486feb51eb4f0db2209fecf/150/150/Hat/Png"}}'''
    
    
    import json
    
    # replace the opening brace of each entry to make a list not a dict
    js = js.replace(":{",":[")
    
    # replace the closing brace of each entryexcept the last entry (which has no comma)
    js = js.replace("},","],")
    
    # replace the last two braces
    js = js[:-2] + "]}"
    
    # this should now work
    json.loads(js)
    

    If this answers your problem, please consider marking this as the solution.

    Login or Signup to reply.
  3. Maybe the sets in your data should really be lists. If that’s the case then you could do this:

    import json
    foo = '''{"3704969059":{"Lunar Moth Headwings",100,1680750150,500,"https://tr.rbxcdn.com/5c0a02da770746e4ac9a1760d427f716/150/150/Hat/Png"},"13114264635":{"Cow Beret",70,1682603172,200,"https://tr.rbxcdn.com/7d39a32f04f00844113f4b3ab355517a/150/150/Hat/Png"},"13160317604":{"Insignia Helmet",2500,1682593511,25,"https://tr.rbxcdn.com/55feabd27486feb51eb4f0db2209fecf/150/150/Hat/Png"}}'''
    foo = foo.replace(':{', ':[').replace('},','],').replace('}}', ']}')
    jdata = json.loads(foo)
    print(json.dumps(jdata, indent=2))
    

    Output:

    {
      "3704969059": [
        "Lunar Moth Headwings",
        100,
        1680750150,
        500,
        "https://tr.rbxcdn.com/5c0a02da770746e4ac9a1760d427f716/150/150/Hat/Png"
      ],
      "13114264635": [
        "Cow Beret",
        70,
        1682603172,
        200,
        "https://tr.rbxcdn.com/7d39a32f04f00844113f4b3ab355517a/150/150/Hat/Png"
      ],
      "13160317604": [
        "Insignia Helmet",
        2500,
        1682593511,
        25,
        "https://tr.rbxcdn.com/55feabd27486feb51eb4f0db2209fecf/150/150/Hat/Png"
      ]
    }
    

    Note:

    This is a massive hack that only works with the data as presented in the question. If the generator of that data was supposed to produce JSON then that’s where the fix needs to be done – not on the client side

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search