how to escape double quotes inside a json - 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.

Related

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

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

NiFi expression language dealing with special characters in JSON keys

So I have some json in which the keys might be something like this:
{
"name" : "John",
"num:itparams:enterprise:2.0:content" : {
"housing" : "5"
},
"num rooms": "12"
}
I get this json from an http request, and I need to use the evaluateJsonPath processor to create attributes from them.
name is easy, i just use $.name
But how would I access the other two? I imagine you would put them in quotes somehow to escape the special characters but just doing $."num:itparams:enterprise:2.0:content" doesnt work.
You can use the bracket for the key-value which has the special characters such as
$.['num:itparams:enterprise:2.0:content'].housing
then it will give you the evaluated result 5.

Regular Expression to extract string from JSON response

I am trying to extract the value from timeZoneId but regexr says no match.
here is the JSON code:
{
"dstOffset": 0,
"rawOffset": 25200,
"status": "OK",
"timeZoneId": "Asia/Jakarta",
"timeZoneName": "Western Indonesia Time"
}
and when I test on regexplanet its says Illegal repetition {\s*"timeZoneId"\s*:\s*(.+?)\s*\}
Can anyone help and explain what i'm doing wrong.
UPDATE
I am parsing the result in a while loop, so I only need the RegEx for the line when it match this "timeZoneId": "Asia/Jakarta", I want to grab out the Asia/Jakarta.
Thanks
G
The { and } brackets are special characters in regex and need to be escaped. See more here: PatternSyntaxException: Illegal Repetition when using regex in Java
Also, as dustmouse said, you are requiring {} around the timeZoneId but there isn't just whitespace between the { and the "timeZoneId". All your other fields are there which is causing it not to match.
There's a lot more than just whitespace between the opening brace and "timeZoneId".
You want: /^\s*"timeZoneId"\s*:\s*(.+?)(?=$|,)/m
But, use a JSON parser. For example jq
echo '{
"dstOffset": 0,
"rawOffset": 25200,
"status": "OK",
"timeZoneId": "Asia/Jakarta",
"timeZoneName": "Western Indonesia Time"
}' | jq -r '.timeZoneId'
Asia/Jakarta

query with filter in JsonPath

I have a structure like this:
{
title: [
{lang: “en”, value: “snickers”},
{lang: “ru”, value: “сниккерс”}
]
}
I need to take a result such this:
“snickers”
I finished with query:
$.title[?(#.lang="en")].value
but it doesn't work
JsonPath always returns an array of results, so you will not be able to get a single value such as "snickers" on its own. You will be able to get ["snickers"] though. To get it working you need to use a double equal sign instead of single:
$.title[?(#.lang=="en")].value
Also note that another reason you may be having issues is that your double quotes in the json are not the standard double quote characters. Depending on how you consume the json you may also need to wrap the property names in double quotes. The json standard is quite strict, and if you are parsing your json from text it has to follow these rules. Here is version of the json corrected in this way:
{
"title": [
{"lang": "en", "value": "snickers"},
{"lang": "ru", "value": "сниккерс"}
]
}
I have tested the above query with the corrected json using http://www.jsonquerytool.com.

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