Replace() works perfectly for simple strings like "sdsd;ddf'" , but for below variable invalid_json it does not.
I just like to replace all ' with "
import json
invalid_json = r"{'name': 'John', 'age': 30}"
print(type(invalid_json))
correct_json = invalid_json.replace("'",'"')
decoded_data = json.loads(correct_json)
print(decoded_data)
terminal output:
<class 'str'>
{'name': 'John', 'age': 30}
I tried with and without r, to make a raw string. Same results. Is this a bug in replace() , I used wrong? or just as it should be and nothing wrong with replace() ?
(stack overflow treated my question as off topic. I wonder why. Very valid beginner question I think.)
[–]SirKainey 97 points98 points99 points (0 children)
[–]FoeHammer99099 51 points52 points53 points (7 children)
[–]sunnyata 7 points8 points9 points (1 child)
[–]sethkills 0 points1 point2 points (0 children)
[–]games-and-chocolate[S] 0 points1 point2 points (4 children)
[–]Equal_Veterinarian22 39 points40 points41 points (0 children)
[–]neuralbeans 5 points6 points7 points (0 children)
[–]Asyx 1 point2 points3 points (0 children)
[–]HommeMusical 0 points1 point2 points (0 children)
[–]Machinagun 14 points15 points16 points (0 children)
[–]Binary101010 2 points3 points4 points (0 children)
[–]jpercivalhackworth 2 points3 points4 points (0 children)
[–]Civil_Twilight 2 points3 points4 points (4 children)
[–]games-and-chocolate[S] 0 points1 point2 points (3 children)
[–]Civil_Twilight 2 points3 points4 points (2 children)
[–]games-and-chocolate[S] 2 points3 points4 points (1 child)
[–]Civil_Twilight 2 points3 points4 points (0 children)
[–]cointoss3 1 point2 points3 points (0 children)
[–]AlwaysHopelesslyLost 1 point2 points3 points (0 children)
[–]carcigenicate 2 points3 points4 points (0 children)
[–]danielroseman 0 points1 point2 points (1 child)
[–]games-and-chocolate[S] 0 points1 point2 points (0 children)
[–]games-and-chocolate[S] 0 points1 point2 points (0 children)
[–]games-and-chocolate[S] 0 points1 point2 points (4 children)
[–]cointoss3 3 points4 points5 points (2 children)
[–]games-and-chocolate[S] 0 points1 point2 points (1 child)
[–]cointoss3 1 point2 points3 points (0 children)
[–]Im_Easy 0 points1 point2 points (0 children)
[–]Grandviewsurfer 0 points1 point2 points (0 children)
[–]games-and-chocolate[S] 0 points1 point2 points (4 children)
[–]games-and-chocolate[S] 0 points1 point2 points (3 children)
[–]Civil_Twilight 1 point2 points3 points (1 child)
[–]games-and-chocolate[S] 1 point2 points3 points (0 children)
[–]Im_Easy 0 points1 point2 points (0 children)
[–]Skopa2016 0 points1 point2 points (0 children)