Json schema ,check double quotes start and end of the string - json

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": "^\".*\"$"
}
}
}

Related

Convert JSON to String Error string literal not terminated [duplicate]

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.

how to escape double quotes inside a json

I'm trying to create a JSON file which will hold a regular expression.
for example:
{
"FrameworksData": [{
"name": "jquery",
"regexes": ["script\s+src\s*=\s*\"jquery", "b", "c"],
"extensions": [".js"],
"conditions":"t1=r1||r2||r3; t2=t1&&e1; res=t2;"
}
]
}
I need to use the double/single quotes as part of the regex that is located inside the JSON. However this JSON is not valid as a result of the structure \"jquery.
how do I use the single quotes and the double quotes in the JSON so I can achieve the correct regex?
Thanks!
Multiple techniques can be adopted:
Escape the backslash \ i.e to \\. For scripted escaping you can use this solution
"FrameworksData": [{"regexes": ["script\\s+src\\s*=\\s*\"jquery", "b", "c"]}]
You can use encodeURIComponent to transfer the regexes if you don't want to escape.

How to declare single and double quote in a json string?

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

Escape dots in Groovy GPath

I am using the RestAssured framework in Java, whose documentation contains this note
Note that the "json path" syntax uses Groovy's GPath notation and is not to be confused with Jayway's JsonPath syntax.
I need to validate the following JSON:
"_source": {
"logSource": {
"logger.name": "LogbackLogger",
},
}
And the selectors like
_source.logSource.logger.name or
_source.logSource.logger.name[0] return no result.
I assume this is due to the dot in the logger.name property.
If no escaping is done, logger.name is interpreted as if name was under the logger, which is not true.
How do I correctly escape the dot character in the GPath, so that logger.name is considered as a single property name?
Thanks!
You have a trivial issue.
Just wrap it in between single quote i.e., 'logger.name'
as there is special character.
Here is complete example :
def string = """{ "_source": {
"logSource": {
"logger.name": "LogbackLogger",
}
}
}"""
def json = new groovy.json.JsonSlurper().parseText(string)
println json.'_source'.logSource.'logger.name'

Passing apostrophe as part of JSON string

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...