1

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

Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
Aarush Kumar
  • 149
  • 1
  • 8
  • Please provide the code that produces this json – ciaran haines Apr 27 '23 at 16:08
  • The problem is not the numbers. The problem is that you have a list of items defined by curly braces instead of by square braces. see this post for guidance https://stackoverflow.com/questions/12288820/what-are-the-differences-between-using-json-arrays-vs-json-objects – ciaran haines Apr 27 '23 at 16:10
  • 1
    doesn't change square brackets give same result – Aarush Kumar Apr 27 '23 at 16:23
  • @ciaranhaines its not technically a code i am using web request – Aarush Kumar Apr 27 '23 at 16:23
  • more like scraping the json from a html source – Aarush Kumar Apr 27 '23 at 16:24
  • 1
    It's not JSON - that's your problem. It looks like a stringified version of a Python dictionary where some values are sets. Consider using literal_eval from the ast module. However, that's likely to introduce another problem (even though it will parse your string) because the values in the sets are unlikely to be in the order you expect – DarkKnight Apr 27 '23 at 16:42

3 Answers3

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

CJC
  • 76
  • 5
1

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.

ciaran haines
  • 294
  • 1
  • 11
1

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

DarkKnight
  • 19,739
  • 3
  • 6
  • 22