A part of my json schema is coming off with backward and forward slash after serialization.
My question is, is this a valid encoding? I'm having issues with the api too for some reason, so trying to see where the problem is.
"_links": {
"altAssetUrl": {
"href": "\/publication\/d40a4e4c-d6a3-45ae-98b3-924b31d8712a\/altasset\/48baad57-81a5-4d32-a2a1-e52c5cbe964d\/"
},
"contentUrl": {
"href": "\/publication\/d40a4e4c-d6a3-45ae-98b3-924b31d8712a\/article\/test\/contents;contentVersion=1521071354969\/"
}
},
In another area I noticed special characters %2F etc.
"socialShareUrl": "https:\/\/example.com\/ssp?entityRef=%2Fpublication%2Fd40a4e4c-d6a3-45ae-98b3-924b31d8712a%2Farticle%2Ftest",
Please advise on what can I do to fix this escaping of slashes, I'm using .net language.
My question is, is this a valid encoding?
Yes.
var json = '"This has a slash\\/"';
console.log("Raw JSON: " + json);
var str = JSON.parse(json);
console.log("String result of parsing JSON: " + str);
In another area I noticed special characters %2F etc.
Perfectly normal URL encoding.
Related
I have a JSON string in Swift 5 that is like this:
var text = """
{"ops": [{"insert": "Hello World!\n"},{"attributes": {"bold": true},"insert": "bold"},{"insert": "What if the "}, {"attributes": {"italic": true},"insert": "italic"}, {"insert": " text was awesome?\n\n"} ]}
"""
I pass that to a WKWebView where it is parsed with JSON.parse(text) so it can be rendered. At present, the JS complains that it's invalid. But if I manually edit the original string and replace all the \n with \\n, it works.
I need to turn \n into \\n programmatically and come out with a string on the other end.
I've tried this:
let raw = #"\#(text)"#
let ready = raw.replacingOccurrences(of: "\n", with: "\\n")
But it treats the newline characters as characters instead of as raw string elements.
It seems like this should be easy, but I can't see what I'm missing. Any ideas?
I found a solution to this. It isn't necessarily an answer to my question, but it avoids the issue altogether.
Prior to sending the JSON data to the Javascript in the WKWebView, I encode it to a string using Base64 encoding. This eliminates the need to worry about escaping characters and such. It works great. :)
I am sending Json to a confluence page but receive a BadRequest error. I have tested extensively and been able to send Json using the same code. The only difference between the json that will not send is that it contains an ampersand &. The Json that will send has escaped the ampersand &.
jsonHolder = {
"id": "....",
"type": "page"......
"body": {
"storage": {
"value": "<table><tbody><tr><td>Row1</td><td>Row2</td><td>Row3</td><td>Empty Sour & Tar</td></tr></tbody></table>",
},
}
This is only a sample of a much larger set and if possable I would only escape & as when I escape the overall json, formatting becomes an issue.
The json is being put into Net::HTTP::Put.new(uri) body.
p jsonHolder = JSON.generate(jsonResult)
request.body = jsonHolder
I have been trying to find a similar examples but many seem to be working on strings, but when I .to_s, the json is parsed and not correct.
I'm passing some data through Json array to an html table. My problem is that the encoding of some frensh letter like (é, è, à),
my json array is like :
paniersFromJsonFile = [
{
"reference": "62010",
"LibelleDeLaPiece": "BOUCLIER",
"origine": "Pièce d'origine",
"distributeur": "datasier"
},
{
"reference": "60100",
"LibelleDeLaPiece": "Grille",
"origine": "Pièce d'origine",
"distributeur": "mvc"
}]
in the screen i have this
Thanks
yes,you need to save file as UTF-8(As gautam said) without BOM (Byte Order Mark in the begining of the file), Notepad++ allows that option
Im trying to parse a string to JSON and I'm getting an unexpected token error.
I am checking validity using http://json.parser.online.fr/ which comes up with no parse errors, but still says the eval fails due to an unexpected token. If you paste the JSON from below in to that website you can see that it finds an error, but doesn't specify what token is causing it.
Heres what I'm trying to parse.
{
"Polish": {
"Rent": [
{
"english": "a",
"audioUrl": "b",
"alternate": "c"
},
{
"english": "d",
"audioUrl": "e",
"alternate": "f"
}
]
}
}
Am I missing something obvious?
EDIT
There is an unprintable character inbetween the : and [ after the "Rent" key.
I am doing some replace() calls on the string prior to the parse attempt which are likely creating the problem.
prior to the parse that particular line is
"Rent":"[
I want to remove the doublequote between the : and [ sybmols.
So I am using:
var reg = new RegExp('":"', 'g');
var newStr = originalStr.replace(reg, '":');
I don't know why the above is causing the unprintable character though.
EDIT2
I did a quick check removing the Above replace() call pasted it into the validator, manually removed the doublequotes I was using replace() on, and the unreadable characters are still there. So the error is present in the original string. So more code :|
The string is being returned from an ajax call to a php script residing on a server. The PHP script is reading a directory on the server and populating nested associative array to produce the string which is sent back to the JS side, which edits and parses it (shown above).
Within the directories are JSON files, which I'm inserting the contents of into this nested array structure to complete the JSON hierarchy.
The unreadable characters were
ef bb bf
Which I googled and found to be the Byte Order Mark of the string representing the file contents.
So heres the PHP Code which reads the directories and JSON files creating a nested array structure to be JSON_encode()d and sent back to the JS
if ($langHandle = opendir($langDir)) {
while (false !== ($langEntry = readdir($langHandle))) {
$currentLangDir = $langDir . "/" . $langEntry;
if (is_dir($currentLangDir) && $langEntry != '.' && $langEntry != '..') {
$currentLang = array();
if ($currentLangHandle = opendir($currentLangDir)) {
while (false !== ($catEntry = readdir($currentLangHandle))) {
$currentCatFile = $currentLangDir . "/" . $catEntry;
if(is_file($currentCatFile) && $catEntry != '.' && $catEntry != '..') {
$currentCat = file_get_contents($currentCatFile);
$currentLang[removeFileExtension($catEntry)] = $currentCat;
}
}
}
$langArray[$langEntry] = $currentLang;
}
}
What can I do to fix these unwanted characters, a quick search on removing the BOM chars suggests it is a bad thing to do.
You probably have a non printable character that is not showing up in what you pasted in your question. I copied and pasted your text into the online parser at the link you provided and it parses cleanly.
Try copying and pasting your original text into this online hex dump website, and compare to what you get when you copy and paste from your SO question above... if they differ then you'll have a clue as to where the bogus character is.
Here's a screenshot of the output I got, which parses cleanly.
Bro, I was having a similar problem, check your file encoding (UTF-8) and (UTF-8 WITHOUT BOM) can make a difference.
I am writing a Groovy script that needs to POST JSON to a URL. I have noticed a problem were all elements in my JSON that contains a '/' are changed to '\/' by the JSON Builder. Is there a way to stop this?
This is using Groovy 1.8. Here is a simple example and its output:
def json = new JsonBuilder()
json.reply {
result 'http://google.ie/testing'
}
println json.toString()
Output -> {"reply":{"result":"http:\/\/google.ie\/testing"}}
Thanks
Just had a look, and groovy.json.JsonOuput.toJson(string) encodes forward slash as '\\/'.
You can use toPrettyString and it doesn't do it however:
def json = new groovy.json.JsonBuilder()
json.reply {
result 'http://google.ie/testing'
}
assert json.toPrettyString() == '''{
"reply": {
"result": "http://google.ie/testing"
}
}'''
Why does the Groovy JSONBuilder escape slashes in URLs?
An excerpt of the interesting points made in http://groups.google.com/group/opensocial-and-gadgets-spec/browse_thread/thread/1642ec0bed9b95ba/21956eed23f04e13?pli=1 on this topic:
Arne Roomann-Kurrik: Per the JSON spec, escaping '/' is optional.
Mike Samuel: The solidus is among the set of characters that MAY be escaped so that it is safe to embed the JSON substring </script> in HTML as <\/script>. (Half of this quote is by Andrea Ercolino.)
Kevin Brown: This is primarily due to buggy javascript parsers that treat // as a comment
when it's in a string.