Regex Get Rid of Hyphens Between Quotes - json

I get a json string back from a device, the string has hyphens in the defintion names and I want to remove that...
Current I have
\"(.+?)\":
But that gets everything within "": I want only the hyphen in there, not all the text. I know I am close just having trouble because regex always confuses me. Below I would like to correct serial-number to serialnumber but not value-2....help!
{
"result": {
"Response": {
"info": {
"serial-number": "xyz",
"value1": "value-2",

You can match
/"([^"]*)-([^"]*)":/
And then replace with just the two submatches.
See http://www.regexpal.com/?fam=95143
This is the sort of js code that should work:
json = json.replace(/"([^"]*)-([^"]*)":/g,'"$1$2"');
Here's another version, using a lookahead:
json = json.replace(/-(?=[^"]*":)/g,"");
This assumes there's never a space between the closing " and the :

Related

Is there a way to put quotation marks inside a json of a json?

I have a Json which contains another Json as a string. I am already able to read the inner Json and getting its contents.
My Json currently looks like this:
[
{
"fullName": "FullNameTest",
"dataType": 3,
"configuration": "[{\"IPAddress\": \"123.123.123\",\"Node\": \"my'node'path'is'here'\"}]"
}
]
As you can see, the second Json is inside of "configuration". The second parameter called "Node" contains a node path which is currently seperated by " ' ". The problem is that I need to replace this with quotation marks because my code will not work otherwise. I also cannot do anything against that.
I am currently replacing the " ' " with some code but I don't think that is a pretty solution.
You can escape it twice, like this:
\\\"text\\\"

VB.NET Can't send POST request properly

So I'm using Newtonsoft.Json to send POST requests.
I have to use the Dictionary to specify my key and value.
Here is how the POST should look like on the receiving end:
{"type":"direct","packages":["http://whatever:9999/something.pkg"]}
My code:
dictData.Add("type", "direct")
dictData.Add("packages", "[http://whatever:9999/something.pkg]")
jsonPost.postData(dictData)
And the output:
{
"type": "direct",
"packages": "[http://whatever:9999/something.pkg]"
}
So the issues: I want no spaces in there, I want it all in one line, and I want the URL in quotation marks. I tried the double "" method, and I got my quotation marks, but it also placed a \ to both the beginning and end of the URL.
I'm sure it is something simple, but for the life of me I could not find anything that worked.
I would simply create a Jobject (first {} ) and add stuff to it. Like:
Dim JSON_Post As New JObject
JSON_Post.Add("type", "direct")
Dim JSON_Array As New JArray
JSON_Array.Add("http://whatever:9999/something.pkg")
'Or JSON_Post.Add("packages", New JArray From {"http://whatever:9999/something.pkg"})
JSON_Post.Add("packages", JSON_Array)
Debug.Print(JSON_Post.ToString)
'Output
{
"type": "direct",
"packages": [
"http://whatever:9999/something.pkg"
]
}
You could do the same with dictionary and list, dictionary is key-value part so it would always look like
"type1": "direct1",
"type2", "direct2"...
So you need to add something with more items (at once), like a list to get the [].
And those spaces are not important, it will be read just fine, it's just how display is formatted.

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.

Invalid JSON but validates on JSONLint

I have the following JSON which validates on JSONLint.com but when I pass it to JSON.parse() I get the error
SyntaxError: JSON.parse: unexpected character
...0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,...
This is apparently the last "correct": line
var theJSON = JSON.parse({
"data": [
{
"wrong": "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0",
"correct": "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
},
{
"wrong": "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0",
"correct": "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
},
{
"wrong": "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0",
"correct": "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
},
{
"wrong": "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0",
"correct": "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
}
]
});
You, like many, have confused JavaScript's literal syntax with JSON. This happens a lot as JSON uses a subset of JavaScript's literal syntax so it looks a lot alike. JSON, however, is always a string. It is a serialized data scheme for porting data structures between langs/platforms.
Also confusing is that a string of JSON which has been output by any platform can be copied and pasted right into JavaScript and used. Again, this is because of the shared syntax. Having pasted such output right into JavaScript, however, one is no longer using JSON--they are now writing JavaScript in literal syntax. That is, unless, you pasted it between quotes and properly escaped the resulting string. But there's no sense in doing so as then it needs to be parsed in order to end up with what you already had.
JSON.parse() is a method for unserializing data which had been serialized into JSON. It expects a string because, well, JSON is a string. You're passing an object (in literal syntax). It does not need parsing...it is already the thing you want.
Wrapping your object literal in single quotes would make the code work, but it would be pointless to do so as the parse would simply result in what you already have.
Your code would be better written if you replaced the variable named theJSON with one named theObject and made it look as such:
var theObject = {
data: [
{
wrong: "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0",
correct: "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
},
{
wrong: "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0",
correct: "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
},
{
wrong: "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0",
correct: "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
},
{
wrong: "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0",
correct: "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
}
]
};
Whatever code wanted to use the parse result should be fine once you've done it.

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