0

I have string objects that I believe are the result of a JSON.stringify() call in javascript. The strings look something like this when printed from my python script:

'{hashtags=[], 
  urls=[{url=https://t.com/H9W2D8GojG, 
         expanded_url=https://twitter.com/i/web/status/1202511173138169856,
         display_url=twitter.com/i/web/status/1…, 
         indices=[117, 140]}
        ]
 }'

I can't just call json.loads() on this string because of the lack of inner quotation marks. Is there any way to turn this into a dictionary without manually parsing it?

theQman
  • 1,690
  • 6
  • 29
  • 52
  • 3
    What you've shown will **never** be the result of calling `JSON.stringify`. `JSON.stringify` might produce [this](https://pastebin.com/eskUtCH6), if the input to it is a string, but it will never produce what's shown in the question. – T.J. Crowder Dec 10 '19 at 18:34
  • Does this answer your question? [How to parse somewhat wrong JSON with Python?](https://stackoverflow.com/questions/1931454/how-to-parse-somewhat-wrong-json-with-python) – luis.parravicini Dec 10 '19 at 18:38
  • @T.J.Crowder: They did say they printed it in Python, which would strip the outer quotation marks. It's likely they `JSON.stringify`-ied a string of pseudo-JSON. – ShadowRanger Dec 10 '19 at 18:42
  • @T.J.Crowder I see. I believe `JSON.stringify` is being used at some point, the string is also saved to an sql database, which I'm reading from. More might be happening behind the scenes, but what I have access to is the string as I showed it. – theQman Dec 10 '19 at 18:42
  • @luis.parravicini I just tried the answers shown there, but none of them worked. – theQman Dec 10 '19 at 18:43
  • @ShadowRanger - I don't see any reason printing it with Python would remove the outer quotes. It's a string, with `"` characters in it. – T.J. Crowder Dec 10 '19 at 18:49
  • @theQman - The point is that there is nothing about that that suggests it's JSON. JSON doesn't look like that at all: 1. JSON uses `:`, not `=`. 2. JSON requires `"` around property keys. 3. JSON requires `"` around strings. [This](https://pastebin.com/LtVA1Jbk) is what it would look like in JSON. Are you sure that this isn't some output representation of a Python object that resulted from **parsing** the JSON? That seems likely...? Or something has, anyway, and then produced this string format. – T.J. Crowder Dec 10 '19 at 18:50
  • Besides the quotes, a : must be used between key and value, instead of =. Doing that replacement, the yaml solution mentioned in the other SO question works with the code you provided. – luis.parravicini Dec 10 '19 at 18:50
  • @T.J.Crowder: I'm referring to the outer quotes you show in your pastebin. Basically, I'm saying yes, creating something like your pastebin by `stringify`-ing something that's already as string (and not valid JSON) is the only reasonable way you'd see what the OP is seeing. – ShadowRanger Dec 10 '19 at 19:20

0 Answers0