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.
Related
I have a JSON file arranged in this pattern:
[
{
"Title ID": "4224031",
"Overtime Status": "Non-Exempt",
"Shift rates": "No Shift rates",
"On call rates": "No On call rates"
},
[
{
"Step: 1.0": [
"$38.87",
"(38.870000)"
]
}
]
][
{
"Title ID": "4225031",
"Overtime Status": "Non-Exempt",
"Shift rates": "No Shift rates",
"On call rates": "No On call rates"
},
[
{
"Step: 1.0": [
"$38.87",
"(38.870000)"
]
}
]
]
I am trying to get it into a Pandas DataFrame. I have tried opening a connection to the JSON file and running JSON.load(s). Unfortunately, I get JSON decode errors like: "JSONDecodeError: Extra data: line 16 column 2 (char 182)". When running the JSON through a linter, I see that there might be an issue with the way the JSON is presented in the file. The parts between the brackets are valid but when wrapped in brackets, become invalid. I have then tried to get at the dictionaries with the wrapping brackets but have not been able to make much progress. Does anyone have tips on how I can successfully access this JSON data and get it into a pandas DataFrame?
The json is invalid beacuase it has more than one root in this representation.
This has to be like this
jsonObject = [{"1":"3"}], [{"4":"5"}]
Hacks that I am able to think of are replace these brackets ][ to this ],[ by find and replace in editor. You'll be able to then create a dataframe as its a list now.
Second, if its not a one time job, then you need to write a regex that can do this for you in text cleaning pipeline(or code). I'm not good at writing of working regex(sorry mate).
I found a solution.
First, after examining the JSON data in a linter, I found that I had some extra brackets and braces at different points. So, I am running the data through a regex that cleans out the unnecessary brackets and braces.
Next, I run each line, which now looks like a string dictionary through json.loads
Finally, I call pd.DataFrame(pd.json_normalize(data)) to get my desired pandas dataframe.
Thanks for the help from commenters.
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\\\"
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 :
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...
This is all you need for valid JSON, right?
["somestring1", "somestring2"]
I'll elaborate a bit more on ChrisR awesome answer and bring images from his awesome reference.
A valid JSON always starts with either curly braces { or square brackets [, nothing else.
{ will start an object:
{ "key": value, "another key": value }
Hint: although javascript accepts single quotes ', JSON only takes double ones ".
[ will start an array:
[value, value]
Hint: spaces among elements are always ignored by any JSON parser.
And value is an object, array, string, number, bool or null:
So yeah, ["a", "b"] is a perfectly valid JSON, like you could try on the link Manish pointed.
Here are a few extra valid JSON examples, one per block:
{}
[0]
{"__comment": "json doesn't accept comments and you should not be commenting even in this way", "avoid!": "also, never add more than one key per line, like this"}
[{ "why":null} ]
{
"not true": [0, false],
"true": true,
"not null": [0, 1, false, true, {
"obj": null
}, "a string"]
}
Your JSON object in this case is a list. JSON is almost always an object with attributes; a set of one or more key:value pairs, so you most likely see a dictionary:
{ "MyStringArray" : ["somestring1", "somestring2"] }
then you can ask for the value of "MyStringArray" and you would get back a list of two strings, "somestring1" and "somestring2".
Basically yes, JSON is just a javascript literal representation of your value so what you said is correct.
You can find a pretty clear and good explanation of JSON notation on http://json.org/
String strJson="{\"Employee\":
[{\"id\":\"101\",\"name\":\"Pushkar\",\"salary\":\"5000\"},
{\"id\":\"102\",\"name\":\"Rahul\",\"salary\":\"4000\"},
{\"id\":\"103\",\"name\":\"tanveer\",\"salary\":\"56678\"}]}";
This is an example of a JSON string with Employee as object, then multiple strings and values in an array as a reference to #cregox...
A bit complicated but can explain a lot in a single JSON string.