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
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": "^\".*\"$"
}
}
}
I have simply defined two variables:
And now I am going to use it inside my request body:
but it doesn't work. Why?
When I enter my values as a string it works perfectly.
postman variables simply resolves to its value , it doesn't differentiate it self as string,number etc . You have to make the decision.
so if a variable as value "test" then :
{
"key":{{var}}
}
resolves to
{
"key":test
}
which is not a valid json , as strings most be enclosed with double quotes (single quotes not supported in json)
solution:
First approach:
enclose all strings with double quotes:
{
"key":"{{var}}"
}
second approach
pass the quotes in value is self:
set value as
pm.environment.set("var",'"test"')
amd im body use without quotes:
{
"key":{{var}}
}
this resolves to :
{
"key":"test"
}
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.
I am unable to convert the following JSON into DataContract Class:
{"SomeData":"Sample={"Id":-1,"Key":"test"}"}
The error shown is:
After parsing a value an unexpected character was encountered: I. Path 'SomeData', line 1, position 22."
Please let me know if it is possible to convert this JSON data into the DataContract class?
You need to use single quotes inside "Sample={ ... }":
{ "SomeData": "Sample={'Id':-1,'Key':'test'}" }
If you need them to be double quotes, you can escape them with \"
{ "SomeData": "Sample={\"Id\":-1,\"Key\":\"test\"}" }
I have a problem that my JSON service is not being called, due to bad format probably.
Still, I dont understand what is wrong it it. I read about it and found out that apostrophes should not be escaped. Also when I escape them, it doesnt work.
"{
"fields": [
{
"Text": "PaymentReminders",
"Value": "'yes'"
}
]
}"
And yes, I really need 'yes' to be under apostrophes.
I am expecting a String on server side, which I then deserialize. It works without apostrophes.
Thanks!
edit1:
This is the structure that accepts in on the server:
Public Class TemplateField
Public Property Value() As String = "val"
Public Property Text() As String = "tex"
End Class
Public Class FieldsList
Public Property fields() As TemplateField()
End Class
and it gets deserialzed like this:
Dim jsSerializer As New JavaScriptSerializer
Dim fieldsArray As EventInfoDetails.FieldsList
fieldsArray = jsSerializer.Deserialize(Of EventInfoDetails.FieldsList)(fields)
and all that works, unless it contains apostrophes. Like I cannot stick apostrophe inside a string.
JSON does not only not require to escape apostrophes, but in fact it does not allow doing so (contrary to JavaScript). So your
"Value": "'yes'"
Is perfectly valid JSON. This is, unless you were inserting this JSON as a String literal inside JavaScript code, in which case it would be JavaScript the one requiring you to escape your ' as \' (you'd need two escapes, the JSON one and the JavaScript one on top of it).
Anyway, there's something strange about your code:
"{
"fields": [
{
"Text": "PaymentReminders",
"Value": "'yes'"
}
]
}"
Why is your entire JSON structure surrounded by quotes (")? Is it a string literal of any kind inside other programming language? In such case, you might need to follow that language's escaping rules for those quote (") symbols. Both Java and VB, for example, would use \" there...