How Do I Turn String Into Dictionary

Question:
I have a string that looks like this: "{"hi":"hello"}" but I want to turn it into a real dictionary! I want the string to turn into a real dictionary like this: {"hi":"hello"}

You could run it through ast’s eval (which is safer than eval itself), or use eval. Do note that eval has security risks, and don’t let users pipe input into it.

How would I do this?

Use json.loads() instead of ast.literal_eval(). While ast.literal_eval() can evaluate a wide range of literal structures (e.g., lists, dictionaries, numbers, strings, booleans, and None), it doesn’t support more complex data types that JSON can handle, such as custom objects or nested structures

import json

string = '{"hi":"hello"}'
dictionary = json.loads(string)

print(dictionary)

It gives me this error (I’m using VSCode btw)

Traceback (most recent call last):
  File "c:\Users\[REDACTED]\OneDrive\Documents\[REDACTED]\main.py", line 118, in <module>
    data = json.loads(line)
           ^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\json\decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
               ^^^^^^^^^^^^^^^^^^^^^^
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
PS C:\Users\[REDACTED]\OneDrive\Documents\[REDACTED]> 
1 Like

What are you trying to load.

The variable in my program is line.

For ast, you’d do this:

from ast import literal_eval

ABC = '{"a": "c"}'
DEF = "{'a': 'c'}"
GHI = "{\"a\": \"c\"}"

ABCDict = literal_eval(ABC)
DEFDict = literal_eval(DEF)
GHIDict = literal_eval(GHI)

You need to ensure that the string is enclosed in double quotes; otherwise, JSON won’t be able to load it.

string = "{'hi':'hello'}"
string = string.replace("'", '"')

Now it says:

Traceback (most recent call last):
  File "c:\Users\[REDACTED]\OneDrive\Documents\[REDACTED]\main.py", line 119, in <module>
    data = json.loads(line)
           ^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\json\decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 146 (char 145)
PS C:\Users\[REDACTED]\OneDrive\Documents\[REDACTED]> 

Can you send the string you’re trying to load.

Oh nvm. The string I was trying to load was broken. Fixed it!

1 Like

@Sky It gives me a new error now. My string is {'Reptile': {'name': 'Reptile', 'date': '2023', 'length': '134 min', 'rating': 'R', 'stars': '6.8', 'people_rated': '47,659', 'metascore': '52'}}:

Traceback (most recent call last):
  File "c:\Users\[REDACTED]\OneDrive\Documents\[REDACTED]\main.py", line 118, in <module>
    data = json.loads(line)
           ^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\json\decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
               ^^^^^^^^^^^^^^^^^^^^^^
json.decoder.JSONDecodeError: Expecting ':' delimiter: line 1 column 1996 (char 1995)
1 Like
import json

input_string = "{'Reptile': {'name': 'Reptile', 'date': '2023', 'length': '134 min', 'rating': 'R', 'stars': '6.8', 'people_rated': '47,659', 'metascore': '52'}}"
input_string = input_string.replace("'", "\"")
input_string = input_string.replace(" ", "")

try:
    data = json.loads(input_string)
    print(data)
except json.JSONDecodeError as e:
    print(f"JSON decoding error: {e}")

@Sky Woops. My real string is:

{'Reptile': {'name': 'Reptile', 'date': '2023', 'length': '134 min', 'rating': 'R', 'stars': '6.8', 'people_rated': '47,659', 'metascore': '52'}, 'Fair Play': {'name': 'Fair Play', 'date': '2023', 'length': '113 min', 'rating': 'R', 'stars': '6.5', 'people_rated': '18,483', 'metascore': '74'}, 'Totally Killer': {'name': 'Totally Killer', 'date': '2023', 'length': '106 min', 'rating': 'R', 'stars': '6.6', 'people_rated': '23,566', 'metascore': '62'}, 'The Exorcist: Believer': {'name': 'The Exorcist: Believer', 'date': '2023', 'length': '111 min', 'rating': 'R', 'stars': '5.1', 'people_rated': '14,497', 'metascore': '39'}, 'The Creator': {'name': 'The Creator', 'date': '2023', 'length': '133 min', 'rating': 'PG-13', 'stars': '7.1', 'people_rated': '43,026', 'metascore': '63'}, 'Killers of the Flower Moon': {'name': 'Killers of the Flower Moon', 'date': '2023', 'length': '206 min', 'rating': 'R', 'stars': '8.2', 'people_rated': '23,258', 'metascore': '90'}, 'Saw X': {'name': 'Saw X', 'date': '2023', 'length': '118 min', 'rating': 'R', 'stars': '6.9', 'people_rated': '26,870', 'metascore': '60'}, 'Haunted Mansion': {'name': 'Haunted Mansion', 'date': '2023', 'length': '123 min', 'rating': 'PG-13', 'stars': '6.1', 'people_rated': '27,629', 'metascore': '47'}, 'Nowhere': {'name': 'Nowhere', 'date': 'II 2023', 'length': '109 min', 'rating': 'TV-MA', 'stars': '6.4', 'people_rated': '20,480', 'metascore': 'N/A'}, 'The Iron Claw': {'name': 'The Iron Claw', 'date': '2023', 'length': '130 min', 'rating': 'N/A', 'stars': 'N/A', 'people_rated': 'N/A', 'metascore': 'N/A'}, 'Mission: Impossible - Dead Reckoning Part One': {'name': 'Mission: Impossible - Dead Reckoning Part One', 'date': '2023', 'length': '163 min', 'rating': 'PG-13', 'stars': '7.8', 'people_rated': '165,932', 'metascore': '81'}, 'The Equalizer 3': {'name': 'The Equalizer 3', 'date': '2023', 'length': '109 min', 'rating': 'R', 'stars': '6.9', 'people_rated': '51,923', 'metascore': '58'}, "Five Nights at Freddy's": {'name': "Five Nights at Freddy's", 'date': '2023', 'length': '110 min', 'rating': 'PG-13', 'stars': 'N/A', 'people_rated': 'N/A', 'metascore': 'N/A'}, 'Barbie': {'name': 'Barbie', 'date': 'I 2023', 'length': '114 min', 'rating': 'PG-13', 'stars': '7.0', 'people_rated': '362,527', 'metascore': '80'}, 'Pet Sematary: Bloodlines': {'name': 'Pet Sematary: Bloodlines', 'date': '2023', 'length': '87 min', 'rating': 'R', 'stars': '4.7', 'people_rated': '5,874', 'metascore': '31'}, 'Talk to Me': {'name': 'Talk to Me', 'date': 'I 2022', 'length': '95 min', 'rating': 'R', 'stars': '7.2', 'people_rated': '83,106', 'metascore': '76'}, 'No One Will Save You': {'name': 'No One Will Save You', 'date': '2023', 'length': '93 min', 'rating': 'PG-13', 'stars': '6.3', 'people_rated': '42,479', 'metascore': '60'}, 'Oppenheimer': {'name': 'Oppenheimer', 'date': 'I 2023', 'length': '180 min', 'rating': 'R', 'stars': '8.5', 'people_rated': '464,616', 'metascore': '88'}, 'The Burial': {'name': 'The Burial', 'date': 'II 2023', 'length': '126 min', 'rating': 'R', 'stars': '7.1', 'people_rated': '9,311', 'metascore': '74'}, 'Sound of Freedom': {'name': 'Sound of Freedom', 'date': '2023', 'length': '131 min', 'rating': 'PG-13', 'stars': '7.8', 'people_rated': '74,881', 'metascore': '36'}, 'Expend4bles': {'name': 'Expend4bles', 'date': '2023', 'length': '103 min', 'rating': 'R', 'stars': '4.9', 'people_rated': '17,753', 'metascore': '30'}, 'The Exorcist': {'name': 'The Exorcist', 'date': '1973', 'length': '122 min', 'rating': 'R', 'stars': '8.1', 'people_rated': '439,473', 'metascore': '82'}, 'The Nun II': {'name': 'The Nun II', 'date': '2023', 'length': '110 min', 'rating': 'R', 'stars': '5.7', 'people_rated': '35,252', 'metascore': '47'}, 'Friday the 13th': {'name': 'Friday the 13th', 'date': '1980', 'length': '95 min', 'rating': 'R', 'stars': '6.4', 'people_rated': '152,508', 'metascore': '22'}, 'Ballerina': {'name': 'Ballerina', 'date': 'III 2023', 'length': '92 min', 'rating': 'TV-MA', 'stars': '6.2', 'people_rated': '5,682', 'metascore': 'N/A'}, 'A Haunting in Venice': {'name': 'A Haunting in Venice', 'date': '2023', 'length': '103 min', 'rating': 'PG-13', 'stars': '6.8', 'people_rated': '32,380', 'metascore': '63'}, 'Pain Hustlers': {'name': 'Pain Hustlers', 'date': '2023', 'length': '122 min', 'rating': 'R', 'stars': '5.8', 'people_rated': '388', 'metascore': '42'}, 'X': {'name': 'X', 'date': 'II 2022', 'length': '105 min', 'rating': 'R', 'stars': '6.6', 'people_rated': '148,696', 'metascore': '79'}, 'Hocus Pocus': {'name': 'Hocus Pocus', 'date': '1993', 'length': '96 min', 'rating': 'PG', 'stars': '6.9', 'people_rated': '150,321', 'metascore': '43'}, 'The Boogeyman': {'name': 'The Boogeyman', 'date': 'I 2023', 'length': '98 min', 'rating': 'PG-13', 'stars': '5.9', 'people_rated': '32,879', 'metascore': '55'}, 'Casper': {'name': 'Casper', 'date': '1995', 'length': '100 min', 'rating': 'PG', 'stars': '6.1', 'people_rated': '142,973', 'metascore': '49'}, 'Taylor Swift: The Eras Tour': {'name': 'Taylor Swift: The Eras Tour', 'date': '2023', 'length': '169 min', 'rating': 'PG-13', 'stars': '8.5', 'people_rated': '7,560', 'metascore': '82'}, 'Elemental': {'name': 'Elemental', 'date': '2023', 'length': '101 min', 'rating': 'PG', 'stars': '7.0', 'people_rated': '86,826', 'metascore': '58'}}

When I run line = line. Replace("'", '"').replace("'", "\"").replace(" ", "") it still gives me an error. It shows up as:

{"Reptile":{"name":"Reptile","date":"2023","length":"134min","rating":"R","stars":"6.8","people_rated":"47,659","metascore":"52"},"FairPlay":{"name":"FairPlay","date":"2023","length":"113min","rating":"R","stars":"6.5","people_rated":"18,483","metascore":"74"},"TotallyKiller":{"name":"TotallyKiller","date":"2023","length":"106min","rating":"R","stars":"6.6","people_rated":"23,566","metascore":"62"},"TheExorcist:Believer":{"name":"TheExorcist:Believer","date":"2023","length":"111min","rating":"R","stars":"5.1","people_rated":"14,497","metascore":"39"},"TheCreator":{"name":"TheCreator","date":"2023","length":"133min","rating":"PG-13","stars":"7.1","people_rated":"43,026","metascore":"63"},"KillersoftheFlowerMoon":{"name":"KillersoftheFlowerMoon","date":"2023","length":"206min","rating":"R","stars":"8.2","people_rated":"23,258","metascore":"90"},"SawX":{"name":"SawX","date":"2023","length":"118min","rating":"R","stars":"6.9","people_rated":"26,870","metascore":"60"},"HauntedMansion":{"name":"HauntedMansion","date":"2023","length":"123min","rating":"PG-13","stars":"6.1","people_rated":"27,629","metascore":"47"},"Nowhere":{"name":"Nowhere","date":"II2023","length":"109min","rating":"TV-MA","stars":"6.4","people_rated":"20,480","metascore":"N/A"},"TheIronClaw":{"name":"TheIronClaw","date":"2023","length":"130min","rating":"N/A","stars":"N/A","people_rated":"N/A","metascore":"N/A"},"Mission:Impossible-DeadReckoningPartOne":{"name":"Mission:Impossible-DeadReckoningPartOne","date":"2023","length":"163min","rating":"PG-13","stars":"7.8","people_rated":"165,932","metascore":"81"},"TheEqualizer3":{"name":"TheEqualizer3","date":"2023","length":"109min","rating":"R","stars":"6.9","people_rated":"51,923","metascore":"58"},"FiveNightsatFreddy"s":{"name":"FiveNightsatFreddy"s","date":"2023","length":"110min","rating":"PG-13","stars":"N/A","people_rated":"N/A","metascore":"N/A"},"Barbie":{"name":"Barbie","date":"I2023","length":"114min","rating":"PG-13","stars":"7.0","people_rated":"362,527","metascore":"80"},"PetSematary:Bloodlines"{"name":"PetSematary:Bloodlines","date":"2023","length":"87min","rating":"R","stars":"4.7","people_rated":"5,874","metascore":"31"},"TalktoMe":{"name":"TalktoMe","date":"I2022","length":"95min","rating":"R","stars":"7.2","people_rated":"83,106","metascore":"76"},"NoOneWillSaveYou":{"name":"NoOneWillSaveYou","date":"2023","length":"93min","rating":"PG-13","stars":"6.3","people_rated":"42,479","metascore":"60"},"Oppenheimer":{"name":"Oppenheimer","date":"I2023","length":"180min","rating":"R","stars":"8.5","people_rated":"464,616","metascore":"88"},"TheBurial":{"name":"TheBurial","date":"II2023","length":"126min","rating":"R","stars":"7.1","people_rated":"9,311","metascore":"74"},"SoundofFreedom":{"name":"SoundofFreedom","date":"2023","length":"131min","rating":"PG-13","stars":"7.8","people_rated":"74,881","metascore":"36"},"Expend4bles":{"name":"Expend4bles","date":"2023","length":"103min","rating":"R","stars":"4.9","people_rated":"17,753","metascore":"30"},"TheExorcist":{"name":"TheExorcist","date":"1973","length":"122min","rating":"R","stars":"8.1","people_rated":"439,473","metascore":"82"},"TheNunII":{"name":"TheNunII","date":"2023","length":"110min","rating":"R","stars":"5.7","people_rated":"35,252","metascore":"47"},"Fridaythe13th":{"name":"Fridaythe13th","date":"1980","length":"95min","rating":"R","stars":"6.4","people_rated":"152,508","metascore":"22"},"Ballerina":{"name":"Ballerina","date":"III2023","length":"92min","rating":"TV-MA","stars":"6.2","people_rated":"5,682","metascore":"N/A"},"AHauntinginVenice":{"name":"AHauntinginVenice","date":"2023","length":"103min","rating":"PG-13","stars":"6.8","people_rated":"32,380","metascore":"63"},"PainHustlers":{"name":"PainHustlers","date":"2023","length":"122min","rating":"R","stars":"5.8","people_rated":"388","metascore":"42"},"X":{"name":"X","date":"II2022","length":"105min","rating":"R","stars":"6.6","people_rated":"148,696","metascore":"79"},"HocusPocus":{"name":"HocusPocus","date":"1993","length":"96min","rating":"PG","stars":"6.9","people_rated":"150,321","metascore":"43"},"TheBoogeyman":{"name":"TheBoogeyman","date":"I2023","length":"98min","rating":"PG-13","stars":"5.9","people_rated":"32,879","metascore":"55"},"Casper":{"name":"Casper","date":"1995","length":"100min","rating":"PG","stars":"6.1","people_rated":"142,973","metascore":"49"},"TaylorSwift:TheErasTour":{"name":"TaylorSwift:TheErasTour","date":"2023","length":"169min","rating":"PG-13","stars":"8.5","people_rated":"7,560","metascore":"82"},"Elemental":{"name":"Elemental","date":"2023","length":"101min","rating":"PG","stars":"7.0","people_rated":"86,826","metascore":"58"}}

Send the full error, the error is probably with the way your JSON data is formatted.

@Sky Here’s the error:

Traceback (most recent call last):
  File "c:\Users\[REDACTED]\OneDrive\Documents\[REDACTED]\main.py", line 119, in <module>
    data = json.loads(line)
           ^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\json\decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
               ^^^^^^^^^^^^^^^^^^^^^^
json.decoder.JSONDecodeError: Expecting ':' delimiter: line 1 column 1758 (char 1757)

Here, I just tested this and this works:

import json

json_data = '''{
    "Reptile": {
        "name": "Reptile",
        "date": "2023",
        "length": "134min",
        "rating": "R",
        "stars": "6.8",
        "people_rated": "47,659",
        "metascore": "52"
    },
    "FairPlay": {
        "name": "FairPlay",
        "date": "2023",
        "length": "113min",
        "rating": "R",
        "stars": "6.5",
        "people_rated": "18,483",
        "metascore": "74"
    },
    "TotallyKiller": {
        "name": "TotallyKiller",
        "date": "2023",
        "length": "106min",
        "rating": "R",
        "stars": "6.6",
        "people_rated": "23,566",
        "metascore": "62"
    },
    "TheExorcist:Believer": {
        "name": "TheExorcist:Believer",
        "date": "2023",
        "length": "111min",
        "rating": "R",
        "stars": "5.1",
        "people_rated": "14,497",
        "metascore": "39"
    },
    "TheCreator": {
        "name": "TheCreator",
        "date": "2023",
        "length": "133min",
        "rating": "PG-13",
        "stars": "7.1",
        "people_rated": "43,026",
        "metascore": "63"
    },
    "KillersoftheFlowerMoon": {
        "name": "KillersoftheFlowerMoon",
        "date": "2023",
        "length": "206min",
        "rating": "R",
        "stars": "8.2",
        "people_rated": "23,258",
        "metascore": "90"
    },
    "SawX": {
        "name": "SawX",
        "date": "2023",
        "length": "118min",
        "rating": "R",
        "stars": "6.9",
        "people_rated": "26,870",
        "metascore": "60"
    },
    "HauntedMansion": {
        "name": "HauntedMansion",
        "date": "2023",
        "length": "123min",
        "rating": "PG-13",
        "stars": "6.1",
        "people_rated": "27,629",
        "metascore": "47"
    },
    "Nowhere": {
        "name": "Nowhere",
        "date": "II2023",
        "length": "109min",
        "rating": "TV-MA",
        "stars": "6.4",
        "people_rated": "20,480",
        "metascore": "N/A"
    },
    "TheIronClaw": {
        "name": "TheIronClaw",
        "date": "2023",
        "length": "130min",
        "rating": "N/A",
        "stars": "N/A",
        "people_rated": "N/A",
        "metascore": "N/A"
    },
    "Mission:Impossible-DeadReckoningPartOne": {
        "name": "Mission:Impossible-DeadReckoningPartOne",
        "date": "2023",
        "length": "163min",
        "rating": "PG-13",
        "stars": "7.8",
        "people_rated": "165,932",
        "metascore": "81"
    },
    "TheEqualizer3": {
        "name": "TheEqualizer3",
        "date": "2023",
        "length": "109min",
        "rating": "R",
        "stars": "6.9",
        "people_rated": "51,923",
        "metascore": "58"
    },
    "FiveNightsatFreddys": {
        "name": "FiveNightsatFreddys",
        "date": "2023",
        "length": "110min",
        "rating": "PG-13",
        "stars": "N/A",
        "people_rated": "N/A",
        "metascore": "N/A"
    },
    "Barbie": {
        "name": "Barbie",
        "date": "I2023",
        "length": "114min",
        "rating": "PG-13",
        "stars": "7.0",
        "people_rated": "362,527",
        "metascore": "80"
    },
    "PetSematary:Bloodlines": {
        "name": "PetSematary:Bloodlines",
        "date": "2023",
        "length": "87min",
        "rating": "R",
        "stars": "4.7",
        "people_rated": "5,874",
        "metascore": "31"
    },
    "TalktoMe": {
        "name": "TalktoMe",
        "date": "I2022",
        "length": "95min",
        "rating": "R",
        "stars": "7.2",
        "people_rated": "83,106",
        "metascore": "76"
    },
    "NoOneWillSaveYou": {
        "name": "NoOneWillSaveYou",
        "date": "2023",
        "length": "93min",
        "rating": "PG-13",
        "stars": "6.3",
        "people_rated": "42,479",
        "metascore": "60"
    },
    "Oppenheimer": {
        "name": "Oppenheimer",
        "date": "I2023",
        "length": "180min",
        "rating": "R",
        "stars": "8.5",
        "people_rated": "464,616",
        "metascore": "88"
    },
    "TheBurial": {
        "name": "TheBurial",
        "date": "II2023",
        "length": "126min",
        "rating": "R",
        "stars": "7.1",
        "people_rated": "9,311",
        "metascore": "74"
    },
    "SoundofFreedom": {
        "name": "SoundofFreedom",
        "date": "2023",
        "length": "131min",
        "rating": "PG-13",
        "stars": "7.8",
        "people_rated": "74,881",
        "metascore": "36"
    },
    "Expend4bles": {
        "name": "Expend4bles",
        "date": "2023",
        "length": "103min",
        "rating": "R",
        "stars": "4.9",
        "people_rated": "17,753",
        "metascore": "30"
    },
    "TheExorcist": {
        "name": "TheExorcist",
        "date": "1973",
        "length": "122min",
        "rating": "R",
        "stars": "8.1",
        "people_rated": "439,473",
        "metascore": "82"
    },
    "TheNunII": {
        "name": "TheNunII",
        "date": "2023",
        "length": "110min",
        "rating": "R",
        "stars": "5.7",
        "people_rated": "35,252",
        "metascore": "47"
    },
    "Fridaythe13th": {
        "name": "Fridaythe13th",
        "date": "1980",
        "length": "95min",
        "rating": "R",
        "stars": "6.4",
        "people_rated": "152,508",
        "metascore": "22"
    },
    "Ballerina": {
        "name": "Ballerina",
        "date": "III2023",
        "length": "92min",
        "rating": "TV-MA",
        "stars": "6.2",
        "people_rated": "5,682",
        "metascore": "N/A"
    },
    "AHauntinginVenice": {
        "name": "AHauntinginVenice",
        "date": "2023",
        "length": "103min",
        "rating": "PG-13",
        "stars": "6.8",
        "people_rated": "32,380",
        "metascore": "63"
    },
    "PainHustlers": {
        "name": "PainHustlers",
        "date": "2023",
        "length": "122min",
        "rating": "R",
        "stars": "5.8",
        "people_rated": "388",
        "metascore": "42"
    },
    "X": {
        "name": "X",
        "date": "II2022",
        "length": "105min",
        "rating": "R",
        "stars": "6.6",
        "people_rated": "148,696",
        "metascore": "79"
    },
    "HocusPocus": {
        "name": "HocusPocus",
        "date": "1993",
        "length": "96min",
        "rating": "PG",
        "stars": "6.9",
        "people_rated": "150,321",
        "metascore": "43"
    },
    "TheBoogeyman": {
        "name": "TheBoogeyman",
        "date": "I2023",
        "length": "98min",
        "rating": "PG-13",
        "stars": "5.9",
        "people_rated": "32,879",
        "metascore": "55"
    },
    "Casper": {
        "name": "Casper",
        "date": "1995",
        "length": "100min",
        "rating": "PG",
        "stars": "6.1",
        "people_rated": "142,973",
        "metascore": "49"
    },
    "TaylorSwift:TheErasTour": {
        "name": "TaylorSwift:TheErasTour",
        "date": "2023",
        "length": "169min",
        "rating": "PG-13",
        "stars": "8.5",
        "people_rated": "7,560",
        "metascore": "82"
    },
    "Elemental": {
        "name": "Elemental",
        "date": "2023",
        "length": "101min",
        "rating": "PG",
        "stars": "7.0",
        "people_rated": "86,826",
        "metascore": "58"
    }
}
'''

try:
    data = json.loads(json_data)
    print(data)
except json.JSONDecodeError as e:
    print(f"JSON decoding error: {e}")

This is my code:

    line =  "{'Reptile': {'name': 'Reptile', 'date': '2023', 'length': '134 min', 'rating': 'R', 'stars': '6.8', 'people_rated': '47,659', 'metascore': '52'}, 'Fair Play': {'name': 'Fair Play', 'date': '2023', 'length': '113 min', 'rating': 'R', 'stars': '6.5', 'people_rated': '18,483', 'metascore': '74'}, 'Totally Killer': {'name': 'Totally Killer', 'date': '2023', 'length': '106 min', 'rating': 'R', 'stars': '6.6', 'people_rated': '23,566', 'metascore': '62'}, 'The Exorcist: Believer': {'name': 'The Exorcist: Believer', 'date': '2023', 'length': '111 min', 'rating': 'R', 'stars': '5.1', 'people_rated': '14,497', 'metascore': '39'}, 'The Creator': {'name': 'The Creator', 'date': '2023', 'length': '133 min', 'rating': 'PG-13', 'stars': '7.1', 'people_rated': '43,026', 'metascore': '63'}, 'Killers of the Flower Moon': {'name': 'Killers of the Flower Moon', 'date': '2023', 'length': '206 min', 'rating': 'R', 'stars': '8.2', 'people_rated': '23,258', 'metascore': '90'}, 'Saw X': {'name': 'Saw X', 'date': '2023', 'length': '118 min', 'rating': 'R', 'stars': '6.9', 'people_rated': '26,870', 'metascore': '60'}, 'Haunted Mansion': {'name': 'Haunted Mansion', 'date': '2023', 'length': '123 min', 'rating': 'PG-13', 'stars': '6.1', 'people_rated': '27,629', 'metascore': '47'}, 'Nowhere': {'name': 'Nowhere', 'date': 'II 2023', 'length': '109 min', 'rating': 'TV-MA', 'stars': '6.4', 'people_rated': '20,480', 'metascore': 'N/A'}, 'The Iron Claw': {'name': 'The Iron Claw', 'date': '2023', 'length': '130 min', 'rating': 'N/A', 'stars': 'N/A', 'people_rated': 'N/A', 'metascore': 'N/A'}, 'Mission: Impossible - Dead Reckoning Part One': {'name': 'Mission: Impossible - Dead Reckoning Part One', 'date': '2023', 'length': '163 min', 'rating': 'PG-13', 'stars': '7.8', 'people_rated': '165,932', 'metascore': '81'}, 'The Equalizer 3': {'name': 'The Equalizer 3', 'date': '2023', 'length': '109 min', 'rating': 'R', 'stars': '6.9', 'people_rated': '51,923', 'metascore': '58'}, "Five Nights at Freddy's": {'name': "Five Nights at Freddy's", 'date': '2023', 'length': '110 min', 'rating': 'PG-13', 'stars': 'N/A', 'people_rated': 'N/A', 'metascore': 'N/A'}, 'Barbie': {'name': 'Barbie', 'date': 'I 2023', 'length': '114 min', 'rating': 'PG-13', 'stars': '7.0', 'people_rated': '362,527', 'metascore': '80'}, 'Pet Sematary: Bloodlines': {'name': 'Pet Sematary: Bloodlines', 'date': '2023', 'length': '87 min', 'rating': 'R', 'stars': '4.7', 'people_rated': '5,874', 'metascore': '31'}, 'Talk to Me': {'name': 'Talk to Me', 'date': 'I 2022', 'length': '95 min', 'rating': 'R', 'stars': '7.2', 'people_rated': '83,106', 'metascore': '76'}, 'No One Will Save You': {'name': 'No One Will Save You', 'date': '2023', 'length': '93 min', 'rating': 'PG-13', 'stars': '6.3', 'people_rated': '42,479', 'metascore': '60'}, 'Oppenheimer': {'name': 'Oppenheimer', 'date': 'I 2023', 'length': '180 min', 'rating': 'R', 'stars': '8.5', 'people_rated': '464,616', 'metascore': '88'}, 'The Burial': {'name': 'The Burial', 'date': 'II 2023', 'length': '126 min', 'rating': 'R', 'stars': '7.1', 'people_rated': '9,311', 'metascore': '74'}, 'Sound of Freedom': {'name': 'Sound of Freedom', 'date': '2023', 'length': '131 min', 'rating': 'PG-13', 'stars': '7.8', 'people_rated': '74,881', 'metascore': '36'}, 'Expend4bles': {'name': 'Expend4bles', 'date': '2023', 'length': '103 min', 'rating': 'R', 'stars': '4.9', 'people_rated': '17,753', 'metascore': '30'}, 'The Exorcist': {'name': 'The Exorcist', 'date': '1973', 'length': '122 min', 'rating': 'R', 'stars': '8.1', 'people_rated': '439,473', 'metascore': '82'}, 'The Nun II': {'name': 'The Nun II', 'date': '2023', 'length': '110 min', 'rating': 'R', 'stars': '5.7', 'people_rated': '35,252', 'metascore': '47'}, 'Friday the 13th': {'name': 'Friday the 13th', 'date': '1980', 'length': '95 min', 'rating': 'R', 'stars': '6.4', 'people_rated': '152,508', 'metascore': '22'}, 'Ballerina': {'name': 'Ballerina', 'date': 'III 2023', 'length': '92 min', 'rating': 'TV-MA', 'stars': '6.2', 'people_rated': '5,682', 'metascore': 'N/A'}, 'A Haunting in Venice': {'name': 'A Haunting in Venice', 'date': '2023', 'length': '103 min', 'rating': 'PG-13', 'stars': '6.8', 'people_rated': '32,380', 'metascore': '63'}, 'Pain Hustlers': {'name': 'Pain Hustlers', 'date': '2023', 'length': '122 min', 'rating': 'R', 'stars': '5.8', 'people_rated': '388', 'metascore': '42'}, 'X': {'name': 'X', 'date': 'II 2022', 'length': '105 min', 'rating': 'R', 'stars': '6.6', 'people_rated': '148,696', 'metascore': '79'}, 'Hocus Pocus': {'name': 'Hocus Pocus', 'date': '1993', 'length': '96 min', 'rating': 'PG', 'stars': '6.9', 'people_rated': '150,321', 'metascore': '43'}, 'The Boogeyman': {'name': 'The Boogeyman', 'date': 'I 2023', 'length': '98 min', 'rating': 'PG-13', 'stars': '5.9', 'people_rated': '32,879', 'metascore': '55'}, 'Casper': {'name': 'Casper', 'date': '1995', 'length': '100 min', 'rating': 'PG', 'stars': '6.1', 'people_rated': '142,973', 'metascore': '49'}, 'Taylor Swift: The Eras Tour': {'name': 'Taylor Swift: The Eras Tour', 'date': '2023', 'length': '169 min', 'rating': 'PG-13', 'stars': '8.5', 'people_rated': '7,560', 'metascore': '82'}, 'Elemental': {'name': 'Elemental', 'date': '2023', 'length': '101 min', 'rating': 'PG', 'stars': '7.0', 'people_rated': '86,826', 'metascore': '58'}}"
    line = line.replace("'", '"').replace("'", "\"").replace(" ", "")
    print(line)
    data = json.loads(line)
    with open('data.json', 'r+') as json_file:
      overide = input("Would You Like To Overide Data?\n")
      if overide.lower() == "yes" or overide.lower() == "y":
        with open('data.json', 'w') as json_file:
          json.dump(data, json_file, indent=4, sort_keys=True)
1 Like

Replace both line with the formatted JSON data I sent above.