How do you represent a JSON array of strings? - json
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.
Related
Is Numeric or integer a valid JSON?
I tested putting just random integer value in a field during a laravel validation. It is returning it as a valid json. Then I tested it at https://jsonlint.com/?code= and that is also returning it as a valid JSON. I am a beginner so Can anyone please explain how it is a valid json?
Very early on, the definition of JSON was that it had to have an object or array at the top level, but that was quickly abandoned in favor of allowing any valid value at the top level. So all of these are valid JSON: A number on its own: 42 A string on its own: "question" A boolean on its own: false An object: {"answer": 42} An array: ["one", "two", "three"] More on json.org and in the standard.
Using JSON: A way to apply a number and a string on same key-value pair?
I need to define one nested JSON object which stores in use case 1 a key value with an integer (amount of something) and in use case 2 a key value with a string (UUID). The goal is to analyse the data in later procedures. I know decided to put the number into quotes and get away with implicit conversion. Which has been described here: Can JSON numbers be quoted? Example-1: "kpiValue": { "type": "Driver", "value": 16 // => amount of something } Example-2 "kpiValue": { "type": "Driver", "value": "ident" // => UUID is handled as string }, Any thoughts on this?
A quoted number is a string. You can safely do this. NB: Don't forget to determine and type your variable property afterward.
How to deserialize json string with an extra ""
I have input Json string that I am trying to deserialize { "ID":1, "Details":{ "Product":""Boston,saline"", "cost":150.0 } } or { "ID":1, "Details":{ "Product":"Boston "Sample"", "cost":150.0 } } When I try to use $JsonConvert.DeserializeObject<JObject>(input) it gives me error saying "After parsing a value an unexpected character was encountered" and this is expected. Is there a way we can deserialize this kind of strings? Thanks!
Could you use .replace from native String method to replace all double double quotes with a single double quote.
You can’t. Say a user is able to create a product named ", "x": "y. This will be represented as (incorrectly serialized to) "Details": { "Product": "", "x": "y", "cost": 150.0 } That is valid JSON, so there’s no syntactic way to detect that anything is wrong. Even worse, what if an attacker creates a product named ", "cost": 1.0, "x": "y. This will be represented as (with some formatting applied) "Details": { "Product": "", "cost": 1.0, "x": "y", "cost": 150.0 } What will your deserializer do with that? Will it accept it? (Some will.) Will it use the first "cost" or the second "cost"—that is, will it allow an attacker to lower the cost? And would it be possible for an attacker to create a duplicate "cost" after the real one? And even if everything is currently okay when deserializing that string, will it continue to work after upgrading to a new version of the library? As of 2017, Json.net accepts such JSON and uses the second duplicate value (see Json.net no longer throws in case of duplicate). But this behavior changed between versions 6 and 8. Will it change again? The only real fix is to fix whatever is generating this JSON.
The stuff in your examples is not JSON; that is why you are having problems parsing it with a JSON parser. Consider fixing either how the data is stored or how it is read. Option 1: Fix how it is stored. It looks like the string value "blammy" is actually stored in your DB as something like: \"blammy\". This is zero percent correct. The value that gets stored in your DB column should be blammy (notice, no quotes). Fix that problem. Option 2: Fix how is is read Something is reading the column value from your DB. Change that thing to remove outer double quotes from string data. Then, the (incorrectly) stored value \"blammy\" would be read (and fixed) to be blammy. This will only work with example 1 in your question. You will need to do something else if you are getting example 2.
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.