Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having an error running loads method #57

Closed
Bright-Fox-Digital-88 opened this issue Jan 28, 2024 · 6 comments · Fixed by #58
Closed

Having an error running loads method #57

Bright-Fox-Digital-88 opened this issue Jan 28, 2024 · 6 comments · Fixed by #58
Assignees

Comments

@Bright-Fox-Digital-88
Copy link

Bright-Fox-Digital-88 commented Jan 28, 2024

My code:

import json
from fuzzy_json import loads

test = "{\"title\": \"Neon Stardom: Cybernetic Chronicles of Future Fame\", \"concept\": \"In the futuristic version of the Hollywood Walk of Fame, known as the Neon Stardom, holographic stars illuminate the sidewalks, each representing a cybernetic performance icon. The city's elite are constantly surrounded by drone-piloted paparazzi, capturing their every move against the dystopian backdrop of the city.\\n\\nOur story follows a talented but disillusioned cybernetic performer, Nova, who rises from the shadows of the city's underbelly to challenge the corrupt system that controls Neon Stardom. As she delves deeper into the dark secrets of the city's elite, she uncovers a conspiracy that threatens to enslave humanity to cybernetic manipulation.\\n\\n\"Neon Stardom: Cybernetic Chronicles of Future Fame\" is a thrilling tale of rebellion, redemption, and the pursuit of truth in a world where fame and power are intertwined with technology. As Nova navigates the treacherous world of Neon Stardom, she must confront her own inner demons and lead a revolution to restore freedom and authenticity to a society dominated by cybernetic performances and drone-piloted surveillance.\"}"

# Process the string to escape internal double quotes properly
parsed_json = loads(test)

# Print the result
print(parsed_json)

# Verify by trying to load the string as JSON
try:
    json.loads(parsed_json)
    print("The string is now valid JSON.")
except json.JSONDecodeError as e:
    print("The string is NOT valid JSON. Error:", e)

My error:

Traceback (most recent call last): File "/Users/gordonligon/Desktop/chain-prompt/test3.py", line 8, in <module> parsed_json = loads(test) File "/Users/gordonligon/Desktop/chain-prompt/lib/python3.10/site-packages/fuzzy_json/decoder.py", line 279, in loads return json.loads(json_str, strict=False) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/__init__.py", line 359, in loads return cls(**kw).decode(s) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/decoder.py", line 353, in raw_decode obj, end = self.scan_once(s, idx) json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 743 (char 742)

I'm a pretty shit programmer, so trying to figure out where i went wrong. The test object is what i receive back from Open AI, + a little .replace to double escape the line breaks.

My tools are breaking whenever GPT decides to throw " in my text.

@lucemia lucemia self-assigned this Jan 29, 2024
@lucemia
Copy link
Contributor

lucemia commented Jan 29, 2024

@Bright-Fox-Digital-88

It seems I made a bad choice initially. The function fuzzy_json.loads operates similarly to json.loads, but it requires manual activation for its distinct features.

To address your issue, modify the line
parsed_json = loads(test)
to
parsed_json = loads(test, auto_repair=True)

This adjustment should resolve the problem.

Regarding the data snippet you provided:
{'title': 'Neon Stardom: Cybernetic Chronicles of Future Fame', 'concept': 'In a future version of the Hollywood Walk of Fame, called Neon Stardom, holographic stars light up the sidewalks, each symbolizing a cybernetic performance icon. The city's high-profile figures are constantly in the limelight, with drone-piloted paparazzi capturing their lives against a dystopian cityscape.\n\nThe narrative centers on Nova, a skilled but disenchanted cybernetic performer who emerges from the city's lower echelons to confront the corrupt forces behind Neon Stardom. Her journey reveals a sinister plot to enslave humanity through cybernetic control.\n\n"Neon Stardom: Cybernetic Chronicles of Future Fame" is an exhilarating story of defiance, redemption, and the quest for truth in a world where fame, power, and technology are closely entwined. As Nova traverses the perilous realm of Neon Stardom, she battles her internal struggles and leads a revolt to bring back freedom and genuineness to a society overwhelmed by cybernetic displays and drone surveillance.'}

I plan to make the necessary updates in the next version of the software.

@lucemia
Copy link
Contributor

lucemia commented Jan 29, 2024

@Bright-Fox-Digital-88

The issue has been resolved in version 0.0.8. Thank you for your report, and if you have any more questions, feel free to reach out.

@Bright-Fox-Digital-88
Copy link
Author

After your suggested change, i managed to produce a new error:

Traceback (most recent call last):
File "/Users/gordonligon/Desktop/chain-prompt/test3.py", line 15, in
json.loads(parsed_json)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/init.py", line 339, in loads
raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not dict

@lucemia
Copy link
Contributor

lucemia commented Jan 29, 2024

@Bright-Fox-Digital-88

Before using json.loads, you must serialize the object.

import json
from fuzzy_json import loads

test = "{\"title\": \"Neon Stardom: Cybernetic Chronicles of Future Fame\", \"concept\": \"In the futuristic version of the Hollywood Walk of Fame, known as the Neon Stardom, holographic stars illuminate the sidewalks, each representing a cybernetic performance icon. The city's elite are constantly surrounded by drone-piloted paparazzi, capturing their every move against the dystopian backdrop of the city.\\n\\nOur story follows a talented but disillusioned cybernetic performer, Nova, who rises from the shadows of the city's underbelly to challenge the corrupt system that controls Neon Stardom. As she delves deeper into the dark secrets of the city's elite, she uncovers a conspiracy that threatens to enslave humanity to cybernetic manipulation.\\n\\n\"Neon Stardom: Cybernetic Chronicles of Future Fame\" is a thrilling tale of rebellion, redemption, and the pursuit of truth in a world where fame and power are intertwined with technology. As Nova navigates the treacherous world of Neon Stardom, she must confront her own inner demons and lead a revolution to restore freedom and authenticity to a society dominated by cybernetic performances and drone-piloted surveillance.\"}"

# Process the string to escape internal double quotes properly
parsed_json = loads(test)

# Print the result
print(parsed_json)

parsed_json_str = json.dumps(parsed_json) # <---------------- add this line

# Verify by trying to load the string as JSON
try:
    json.loads(parsed_json_str)
    print("The string is now valid JSON.")
except json.JSONDecodeError as e:
    print("The string is NOT valid JSON. Error:", e)

@Bright-Fox-Digital-88
Copy link
Author

Success! Thank you!

@lucemia
Copy link
Contributor

lucemia commented Jan 29, 2024

Please star this GitHub repository if you feel it is useful:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants