1

How to convert this string into json in python?

>>> data = '[for: css=a[title="See LLCAutoSept files"]]'
>>> json.dumps(data)
'"[for: css=a[title=\\"See LLCAutoSept files\\"]]"'

I've tried using json.dumps(), however the output json is could not be validated on jsonlint.com

user3388884
  • 4,748
  • 9
  • 25
  • 34

3 Answers3

5

You are looking at the Python string representation. It's valid Python code, containing valid JSON.

Print the value:

>>> imp
>>> data = '[for: css=a[title="See LLCAutoSept files"]]'
>>> print json.dumps(data)
"[for: css=a[title=\"See LLCAutoSept files\"]]"

JSONlint is using the older, stricter RFC 4627 that requires the top level to be an array or object, so it won't validate a JSON string.

However, Python's json.dumps() produces valid RFC 7159 output.

If your application requires JSONlint compliance, then by all means add a list or dictionary:

>>> print json.dumps([data])
["[for: css=a[title=\"See LLCAutoSept files\"]]"]
>>> print json.dumps({'data': data})
{"data": "[for: css=a[title=\"See LLCAutoSept files\"]]"}

JSONlint.com validates either of these as valid.

There was, for a time, quite some confusion over this, with RFC 4627, ECMA-262 and ECMA-404 and actual implementations disagreeing over what was allowed; at least RFC 7159 agrees with ECMA-404 now on this. Also see What is the minimum valid JSON?

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • i think the problem is that jsonlint.com won't accept string only json – Bastian Jul 10 '14 at 15:15
  • @Bastian: I am addressing that, yes. – Martijn Pieters Jul 10 '14 at 15:18
  • Thanks for all the inputs. You guys are correct. If i print the json.dumsp(data), i only get 1 slash. But if I don't print it, why does the interpreter output the string with double slashes? see my original example. Thanks! – user3388884 Jul 10 '14 at 22:06
  • @user3388884: because you are given a *Python string representation*, for ease of debugging. You can paste that representation back into the interpreter and it'll *recreate the original string value*. – Martijn Pieters Jul 10 '14 at 22:08
  • @user3388884: it is the equivalent of `print repr(json.dumps(data))`. – Martijn Pieters Jul 10 '14 at 22:09
1

This is not valid json because it does not have a key for the array and opening and closing curly or square brackets, that is why you cannot validate it on jsonlint.com

heinst
  • 8,520
  • 7
  • 41
  • 77
  • JSON does not require the top level to be an object. – Martijn Pieters Jul 10 '14 at 15:12
  • Hence why I said `opening and closing curly or square brackets` meaning it can be an object OR array @MartijnPieters so if you could please change your vote that'd be great. Thanks – heinst Jul 10 '14 at 15:15
  • 1
    That's still only true for the older (superseded) [RFC 4627 standard](http://tools.ietf.org/html/rfc4627); [RFC 7158](http://tools.ietf.org/html/rfc7158) allows for any JSON type as the top level. – Martijn Pieters Jul 10 '14 at 15:19
  • 1
    @MartijnPieters that's what heinst said. He doesn't contradict you, you don't get his message. It's not about the top level. It simply isn't json – Ivo Jul 10 '14 at 15:22
  • @IvoBeckers: It's a plain string, dumped to JSON. The string contents are not valid JSON, but the output of `json.dumps()` *is*. But JSONlint doesn't validate that either because *it* is following an older outdated RFC. – Martijn Pieters Jul 10 '14 at 15:26
  • @Bastian: hehehe, thanks. Still, March 2014, that's new enough. – Martijn Pieters Jul 10 '14 at 15:28
  • O now, I see. There's been a slight misunderstanding here. My point (and also heinst's I think) was that the original string is not valid json, not the dumped one – Ivo Jul 10 '14 at 15:29
  • @Bastian: I feel cheated now. 7158 and 7159 differ only in the date of publication and the RFC number. Oh, and 7158 has been marked as superseded by 7159... *There are no other differences*. – Martijn Pieters Jul 10 '14 at 15:32
1

This works for me

data = {'string':'[for: css=a[title="See LLCAutoSept files"]]'}
print(json.dumps(data))
Trylks
  • 1,458
  • 2
  • 18
  • 31