I have some json where the backslash is escaped like this
"b\\ar"
When I get the data back from fetch, the object looks like this:
{ foo: "b\ar" }
You can see that it has removed one of the backslashes
Problem is that if I search for a backslash in foo.bar it can't find one
because it thinks that it's an escape character called '\a'
Help please!!!
Related
JSON.parse('["foo", "bar\\"]'); //Uncaught SyntaxError: Unexpected end of JSON input
When I look at the above code everything seems grammatically correct. It's a JSON string that I assumed would be able to be converted back to an array that contains the string "foo", and the string "bar\" since the first backslash escapes the second backslash.
So why is there an unexpected end of input? I'm assuming it has something to do with the backslashes, but I can't figure it out.
It seems like your code should be:
JSON.parse('["foo", "bar\\\\"]');
Your Json object is indeed ["foo", "bar\\"] but if you want it to be represented in a JavaScript code you need to escape again the \ characters, thus having four \ characters.
Regards
You'd need to double escape. With template literals and String.raw you could do:
JSON.parse(String.raw`["foo", "bar\\"]`);
I have a string that is automatically generated by some code that encodes a Google Static Map polyline set of longitude,latitude. However, it places these pesky backslashes in the string which tries to escape the character after it.
enc:{eggEhwnQDYOCZuDv#q#H}#v#k#^v#TQh#Aw#j#AJJ#CZKA?LNZ[RUEALDDCRC#AJBBCJ\FAEACC?GM?K#GDGDEJC#BDADBFB#F#HANGH?DB#D\W|#g#QIZm#I#YoAO
I am not putting the encode directly into the HTML (where it would be fine) but instead using JavaScript to do it so this polyline encode gets put into a variable like so:
mapcoords:"path=color:0x00000000|fillcolor:0xFF9999|enc:{eggEhw`nQDYOCZuDv#q#H}#v#k#^v#TQh#`Aw#j#AJJ#CZKA?LNZ[RUEALDDCRC#AJBBCJ\FAEACC?GM?K#GDGDEJC#BDADBFB#F#HANGH?DB#D\W|#g#QIZm#I#YoAO"
Any suggestions how I go around the escaping? I've looked for the ampersand symbol for backslash but it seems one does not exist (if it would even help). So I am not sure how else to go about this.
You have to escape the backslash with a backslash like this:
var some_string = "my string with a backslash here: \\ ";
Most editors today have a find/replace function that you can use to replace a single backslash with two backslashes. If you use Notepad++ you can use CTRL+H to access this function, but as I said, most recent editors and IDE's have this function.
All you need to do is escape the escape character, so you simply get \\ instead of a single \. You will have to do this replacement wherever it is you're outputting that string to the client.
I'm trying to write a regex in javascript to identify string representations of arbitrary javascript functions found in json, ie. something like
{
"key": "function() { return 'I am a function'; }"
}
It's easy enough to identify the start, but I can't figure out how to identify the ending double quotes since the function might also contain escaped double quotes. My best try so far is
/"\s*function\(.*\)[^"]*/g
which works nicely if there are no double quotes in the function string. The end of a json key value will end with a double quote and a subsequent comma or closing bracket. Is there some way to retrieve all characters (including newline?) until a negated pattern such as
not "/s*, and not "/s*}
... or do I need to take a completely different approach without regex?
Here's is the current test data I'm working with:
http://regexr.com/39pvi
Seems like you want something like this,
"\s*function\(.*\)(?:\\.|[^\\"])*
It matches also the inbetween \" escaped double quotes.
DEMO
I'm trying to stringify a JSON object that contains a string with quotes inside of it:
array = ['bar "foo"']
However, the string is created as: '["bar \\"foo\\""]' when I was hoping for something more along the lines of '["bar \"foo\""]'. Why are there two backslashes generated? Thanks
Why are there two backslashes generated?
Because backslashes must be escaped by backslashes to represent one single backslash in a string literal.
The string
'["bar \\"foo\\""]'
// or
"[\"bar \\\"foo\\\"\"]"
represents the value
["bar \"foo\""]
which is JSON for an array object containing the string value bar "foo".
Probably the confusion was caused when you expected to see the value but the tool you used for that printed the string literal.
console.log(JSON.parse('{"data":"{\"json\":\"rocks\"}"}'));
gives error (tested on Firefox and Chrome's console). Is this a bug with JSON.parse? Same decodes well when tested with PHP.
print_r(json_decode('{"data":"{\"json\":\"rocks\"}"}', true));
This string is processed differently in PHP and JS, i.e. you get different results.
The only escapes sequences in single quoted strings in PHP are \\ and \'. All others are outputted literally, according to the documentation:
To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash, double it (\\). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.
In JS on the other hand, if a string contains an invalid escape sequence, the backslash is discarded (CV means character value):
The CV of CharacterEscapeSequence :: NonEscapeCharacter is the CV of the NonEscapeCharacter.
The CV of NonEscapeCharacter :: SourceCharacter but not EscapeCharacter or LineTerminator is the SourceCharacter character itself.
The quote might not be helpful by itself, but if you follow the link and have a look at the grammar, it should become clear.
So in PHP the string will literally contain \" while in JS it will only contains ", which makes it invalid JSON:
{"data":"{"json":"rocks"}"}
If you want to create a literal backslash in JS, you have to escape it:
'{"data":"{\\"json\\":\\"rocks\\"}"}'
To have a literal backslash in a string literal,you need \\.
console.log(JSON.parse('{"data":"{\\"json\\":\\"rocks\\"}"}'));
This will successfully escape the inner quotation marks for the JSON processing.
You need to escape the backslashes:
console.log(JSON.parse('{"data":"{\\"json\\":\\"rocks\\"}"}'));
object with one or more then '\' wont return Object by JSON.parser, It will return the string again with skipping one '\'.
You can do parse again and again until all '\' skipped.
myobj = {\"json\":\"rocks\"}
myobj = {\\"json\\":\\"rocks\\"}
Following lines worked for me
remove backslash
while(typeof myobj == 'string'){
myobj = JSON.parse(myobj)
}
You don't really need to escape double quotes inside single quotes and you have two extra quotes in your input around inner object, just
console.log(JSON.parse('{"data":{"json":"rocks"}}'));
is enough.