Can I enter special characters from a JSON?

10

How can I enter special characters within a JSON? For example, if I have:

{
    "a": "Hola mundo"
}

and I want to put the word "world" in quotes. Try escaping them and with the HTML code: " The string I show in a web page, but neither of the two options worked, I do not know if the library that loads it or is something inherent in the use of JSON.

  

Edited:

Escaped quotes: \"

Do not adequately pose the question. I see that my problem is a thing of the library ngx-translate for Angular2 , not for the JSON itself. This library stores the translations in the JSON file, but it has not allowed me to enter double quotes within them as I indicated before.

    
asked by Orici 30.05.2018 в 23:04
source

4 answers

13

According to the standard used by JSON.org for the use of strings or String , Values should start with a " to use a valid character except double quotes should use the backslash \ and after that the control character that you need obviously ending with "

Some valid JSON

let comillaDoble = {
    "a": "comilla \"doble\""
}
let comillaSimple = {
    "a": "comilla \'simple\'"
}
let saltoCarro = {
    "a": "\t tab \n Enter"
}
let barras = {
    "a": "barra1\\/barra2 \u00F1"
}

console.log(comillaDoble.a,comillaSimple.a)
console.log(saltoCarro.a)
console.log(barras.a)

Now what does that \u mean, what's it for? simple is used for special characters call accents and accents among others in unicode format .

miJson = {
"resultado":  "1 es \u003C 2 ", 
}
console.log(miJson.resultado)

An example of when we use Unicode is when we send the answers via GET the parameter is transformed to unicode with the command encodeURIComponent

  

I invite you to read the differences between a JSON and an Object in JavaScript

    
answered by 31.05.2018 / 00:50
source
8

If you can, you should only use a backslash ( \ ) in front of the quotes, it will not be valid if you do not have opening and closing quotes.

Asi:

{
   "a": "Hola \"mundo\""
}
    
answered by 30.05.2018 в 23:11
4

I leave you with these two solutions:

{
    "a": "Hola \"mundo\""
}

Otherwise you can try:

{
    'a': 'Hola "mundo"'
}

And if you experience any other problem, use the JSON.parse function, once you have your object correctly stored in your script.

    
answered by 30.05.2018 в 23:16
0

In JSON, as with any other string, there are escape chains that allow the insertion of special characters such as: the line break, the tabulation, the tildes, among others ... And for this particular cas, you must place the backslach followed by the character that you want to take as a string (\ ") ...

{
    "a": "\"Hola mundo\""
}
    
answered by 09.06.2018 в 19:45