This question already has answers here:
How do you write multiline strings in Go?
(12 answers)
Closed 3 years ago.
When I try to run given code I get the error :: string literal not terminated (illegal character U+005C '\') . How do I fix the given code?
payload := "{
\"key_id\":\"3\",
\"contacts\":[
{
\"external_id\":\"chandan4u1990#gmail.com\",
\"data\":{
\"global\":{
\"name\":\"Adoni Mishra\"
}
}
},
{
\"external_id\":\"chandankumarc#airasia.com\",
\"data\":{
\"global\":{
\"name\":\"CHANDAN KUMAR\"
}
}
}
]
}"
When I concat all the lines in one then it starts working::
payload := "{\"key_id\":\"3\",\"contacts\":[{\"external_id\":\"chandan4u1990#gmail.com\",\"data\":{\"global\":{\"name\":\"Adoni Mishra\"}}},{\"external_id\":\"chandankumarc#airasia.com\",\"data\":{\"global\":{\"name\":\"CHANDAN KUMAR\"}}}]}"
You are using an interpreted string literal which may not contain newlines! Spec: String literals:
Interpreted string literals are character sequences between double quotes, as in "bar". Within the quotes, any character may appear except newline and unescaped double quote.
Use a raw string literal so you don't even have to escape quotes, it will be much more readable, and newlines are allowed in raw string literals:
Raw string literals are character sequences between back quotes, as in foo. Within the quotes, any character may appear except back quote.
For example:
payload := `{
"key_id":"3",
"contacts":[
{
"external_id":"chandan4u1990#gmail.com",
"data":{
"global":{
"name":"Adoni Mishra"
}
}
},
{
"external_id":"chandankumarc#airasia.com",
"data":{
"global":{
"name":"CHANDAN KUMAR"
}
}
}
]
}`
You can also put everything in one line if you don't need indentation:
payload := `{"key_id":"3","contacts":[{"external_id":"chandan4u1990#gmail.com","data":{"global":{"name":"Adoni Mishra"}}},{"external_id":"chandankumarc#airasia.com","data":{"global":{"name":"CHANDAN KUMAR"}}}]}`
Try it on the Go Playground.
Related
I have a json property Employee-name which contains a string with double quotes at the start and end like this:
"ramesh yadav"
I have a schema.json file to validate schema of my json data.
I wanna know how I can write regular expression inside schema file so it will check for double quotes
"Employee-name": {
"type":"string",
"pattern":??????
}
You use the line start (^) and end ($) regex operands together with escaped (via \) double quotes to make the regex a valid JSON string. All put together, the following should work:
{
"properties": {
"Employee-name":{
"type":"string",
"pattern": "^\".*\"$"
}
}
}
Golang json.Unmarshal throws error for newline character. Go Playground
How to unmarshal data if string contains newline?
Simply escaping the newline character should do the trick:
var val []byte = []byte(`"{\"channel\":\"buupr\\niya\",\"name\":\"john\", \"msg\":\"doe\"}"`)
The output for the above:
{"channel":"buupr\niya","name":"john", "msg":"doe"}
Since you're attempting to pass a raw string literal here, you will need to be able to represent the JSON in string form, which requires you to escape the newline character.
I have declared a property in my json file of string type and I have both single and double quotes inside that string like below-
{
"height": "5' 8" "
}
So, now it is showing some error. How should I write that?
The single quote is not an issue, you have to escape the double quote like this:
{
"height": "5' 8 inch\" "
}
See the spec: http://json.org
Below format should work for you. As your Json element has double quote inside the actual string, you can use backslash escape character.
{
"height": "5' 8 inch\" "
}
For more details, you can refer the link.
https://www.freeformatter.com/json-escape.html
I'm attempting to read the following JSON file ("my_file.json") into R, which contains the following:
[{"id":"484","comment":"They call me "Bruce""}]
using the jsonlite package (0.9.12), the following fails:
library(jsonlite)
fromJSON(readLines('~/my_file.json'))
receiving an error:
"Error in parseJSON(txt) : lexical error: invalid char in json text.
84","comment":"They call me "Bruce""}]
(right here) ------^"
Here is the output from R escaping of the file:
readLines('~/my_file.json')
"[{\"id\":\"484\",\"comment\":\"They call me \"Bruce\"\"}]"
Removing the quotes around "Bruce" solves the problem, as in:
my_file.json
[{"id":"484","comment":"They call me Bruce"}]
But what is the issue with the escapement?
In R strings literals can be defined using single or double quotes.
e.g.
s1 <- 'hello'
s2 <- "world"
Of course, if you want to include double quotes inside a string literal defined using double quotes you need to escape (using backslash) the inner quotes, otherwise the R code parser won't be able to detect the end of the string correctly (the same holds for single quote).
e.g.
s1 <- "Hello, my name is \"John\""
If you print (using cat¹) this string on the console, or you write this string on a file you will get the actual "face" of the string, not the R literal representation, that is :
> cat("Hello, my name is \"John\"")
Hello, my name is "John"
The json parser, reads the actual "face" of the string, so, in your case json reads :
[{"id":"484","comment":"They call me "Bruce""}]
not (the R literal representation) :
"[{\"id\":\"484\",\"comment\":\"They call me \"Bruce\"\"}]"
That being said, also the json parser needs double-quotes escaping when you have quotes inside strings.
Hence, your string should be modified in this way :
[{"id":"484","comment":"They call me \"Bruce\""}]
If you simply modify your file by adding the backslashes you will be perfectly able to read the json.
Note that the corresponding R literal representation of that string would be :
"[{\"id\":\"484\",\"comment\":\"They call me \\\"Bruce\\\"\"}]"
in fact, this works :
> fromJSON("[{\"id\":\"484\",\"comment\":\"They call me \\\"Bruce\\\"\"}]")
id comment
1 484 They call me "Bruce"
¹
the default R print function (invoked also when you simply press ENTER on a value) returns the corresponding R string literal. If you want to print the actual string, you need to use print(quote=F,stringToPrint), or cat function.
EDIT (on #EngrStudent comment on the possibility to automatize quotes escaping) :
Json parser cannot do quotes escaping automatically.
I mean, try to put yourself in the computer's shoes and image you should parse this (unescaped) string as json: { "foo1" : " : "foo2" : "foo3" }
I see at least three possible escaping giving a valid json:
{ "foo1" : " : \"foo2\" : \"foo3" }
{ "foo1\" : " : "foo2\" : \"foo3" }
{ "foo1\" : \" : \"foo2" : "foo3" }
As you can see from this small example, escaping is really necessary to avoid ambiguities.
Maybe, if the string you want to escape has a really particular structure where you can recognize (without uncertainty) the double-quotes needing to be escaped, you can create your own automatic escaping procedure, but you need to start from scratch, because there's nothing built-in.
I'm developing a small tool written in Groovy which parses JSON strings in emails. Some of these JSON strings have JSON values that contain escaped quotes.
For example:
{
"hello": "world with \"quotation marks\""
}
To parse these strings, I am using Groovy's JsonSlurper. The following code demonstrates my problem:
import groovy.json.JsonException
import groovy.json.JsonSlurper
try {
print new JsonSlurper().parseText('''
{
"hello": "world with \"quotation marks\""
}
''')
} catch (JsonException | IllegalArgumentException e) {
print e
}
See https://groovyconsole.appspot.com/script/6193189027315712 for a live demo.
When executing this code, the following exception is thrown:
groovy.json.JsonException: expecting '}' or ',' but got current char 'q' with an int value of 113
The current character read is 'q' with an int value of 113
expecting '}' or ',' but got current char 'q' with an int value of 113
line number 3
index number 35
"hello": "world with "quotation marks""
............................^
Thus, the escaping of the quotation mark is ignored by JsonSlurper. Unfortunately, I have no control over the input, i.e. the JSON strings. Therefore, I have to find a way to parse such a JSON string into a map or any other appropriate data structure.
The string has not been escaped properly in the json. The text data should be like:
'''
{
"hello": "world with \\\"quotation marks\\\""
}
'''
The string that you are getting indicates that the mail body contains the json in format:
{
"hello": "world with "quotation marks""
}
while it should be like
{
"hello": "world with \"quotation marks\""
}
If earlier is the case then you can't parse the invalid json as there is no way for code to identify about the escaped data.