Python json dumps single backslash
I need a JSON HTTP response to be as follows:
I have a python dict
Now when I return the dict using flask I am passing it through You are wondering why I would ever want to return a crazy result like this? Well that is how Kendo Scheduler expects date objects. See http://demos.telerik.com/kendo-ui/scheduler/index Any help would be appreciated. Thanks! asked Sep 17, 2014 at 20:39
JakobovskiJakobovski 2,9351 gold badge28 silver badges37 bronze badges 6 You are seeing an escaped forward slash. The JSON standard allows any character to be escaped, and the forward slash can be escaped by preceding it with a backward slash. See the Strings section of RFC 7159. So
Note how the Your browser does the same thing; when any compliant JSON value which includes strings containing Just provide the value in Python with the forward slashes.
answered Sep 18, 2014 at 9:21
Martijn Pieters♦Martijn Pieters 983k272 gold badges3871 silver badges3232 bronze badges 0 While Martijn is absolutely correct, he does include a caveat about the far end being compliant. If it isn't, like JustGiving, then you might find the following a useful starting point:
answered Apr 7, 2016 at 11:23
Not the answer you're looking for? Browse other questions tagged python json kendo-ui flask or ask your own question.I am using jsonnet to write configurations for drone ci. Drone uses jsonnet to generate yaml file and use it as instruction for CI/CD. For my needs I want to have the following output: { "some-field" : "${DRONE_BRANCH/\//-}" } I tried the following jsonnet script: { "some-field": "${DRONE_BRANCH/\//-}", } And it gave me: { "some-field": "${DRONE_BRANCH///-}" } Then I tried: { "some-field": "${DRONE_BRANCH/\\//-}" } It gave me: { "some-field": "${DRONE_BRANCH/\\//-}" } I did not find a way to achieve what I want. Seems to me it's currently impossible. P.S. I am using the latest version of jsonnet $ jsonnet -v Jsonnet commandline interpreter v0.13.0 alikhil, Raiondesu, prokop7, kevinkhanda, nickaleks, IvanovaChristina, Vsnegovik, gadhagod, and gaurav517 reacted with thumbs up emojiRaiondesu, kevinkhanda, Vsnegovik, and gaurav517 reacted with rocket emoji +10 for this. How come single slashes are not allowed in a JSON? The thing is that in JSON Similarly JSON strings Python agrees btw:
Escaping Please let me know if that answers your question. @sbarzowski, though it's a comprehensive answer, I suggest you pay more attention when reading the question.
In other words, escaping the escaping backslash just produces two backslashes. Therefore, the last bit of "example output" you've provided is not how jsonnet handles this. Instead, it actually produces
Did you actually run it in Python? This was directly copy&pasted from Python shell. Here you can see it yourself: https://www.onlinegdb.com/S1g978ilH.
Yes, because
it also needs to be escaped in the output JSON. I'm arguing that it is the correct behavior. And that trying to get JSON output For completeness I'll add that you can use the "raw string" mode ( In other words if you want it to be loaded with a single backslash you need these two backslashes in JSON. E.g.:
With a single backslash it won't
work as expected (it will be interpreted as escaped
I'm going to assume that it's all clear now after my last explanation. If it's not, feel free to reopen. Just came here to add that in YAML, you can specify a single character string containing a backslash as either: or In JSON, only the latter is supported. When Jsonnet generates YAML with Oh and just for completeness -- the various ways of specifying it in Jsonnet: [ "\\", '\\', @"\", @'\', // Note, this last one also adds a \n, see #289 ||| \ |||, ] |