Can we write json key with space like(first name) - json

Can we write json key with space like first name?
Below is the sample json:
[
{
"first_name": "James"
},
{
"first_name": "Tom"
}
]

Yes, you can, it is valid RFC 8259, however it will make more difficult to use in some languages, for example in PHP you want be able to use the arrow operator to access the data.
This obviously won't make this JSON not valid.

Related

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.

JSON schema conditional check in JSON object within array

Here is the desired schema and json for illustration purpose. Please see the link below.
JSON Schema and JSON
{
"id": "123" ,
"ts": "1234567890",
"complex_rules":
[
{
"type":"admin",
"rule":{
"rights":"all",
"remarks": "some admin remarks"
}
},
{
"type":"guest",
"rights": "limited"
},
{
"type":"anonymous",
"rights": "blocked"
}
]
}
The 'complex_rules' is an array of json object:
With type either be a : "admin", "guest", "anonymous" and the 'type' attribute is MANDATORY.
Each object in array can have its own structure, but the type can be either of: "admin", "guest", "anonymous" only. No other type attribute is acceptable.
The conditions to evaluate:
The type of object in the array cannot re-occur in the array. (I know this seems to be not possible, so we can ignore this)
If attribute "rights" in the {type=admin object} with any value, then we cannot have "rights": "limited" or any value in {type=guest object}. The JSON Schema validation must complain about this.
Another twist, either object {type":"guest"}or {type":"anonymous"} can exist. Both types cannot coexist along with other types.
----Update
The above link is the solution this question.
In regards to 1 and 2:
You need to use a combination of if, then, and not keywords to construct the logic you require with the correct level of applicability.
In regards to 3:
The type of object in the array cannot re-occur in the array. (I know
this seems to be not possible, so we can ignore this)
Right, that's correct, it's not possible as of draft-7 JSON Schema.

Swift 4: Keeping the same order when parsing a JSON

I need to show a list of addresses in my app while keeping the same order as the response JSON. It looks something like this:
{
"addresses": {
"e5fdb5ba-7afb-11e8-bead-43321d1a8905": {
"contact": "Name1",
"zipCode": "06100"
},
"1495b580-7afc-11e8-bead-9f4049791561": {
"contact": "Name2",
"zipCode": "06100"
},
"e5fdb5ba-7afb-11e8-bead-43321d1a8905": {
"contact": "Name3",
"zipCode": "06100"
},
"1495b580-7afc-11e8-bead-9f4049791561": {
"contact": "Name4",
"zipCode": "06100"
}
}
}
But when I parse the response using
JSONSerialization.jsonObject(with: data, options: [])
The order of the addresses gets all mixed up. Is there a way to keep the exact same order as the JSON?
as rmaddy says in their comment, the JSON you posted is a dictionary. Dictionaries are inherently unordered, so the order you get from the back-end is not guaranteed either. Some server implementations may give a consistent order and some may not.
If you want a certain order you will need to change the back-end system to use an array rather than a dictionary.
If you have no control of the client side and still want to preserve the order of the keys from the JSON, you will probably have to parse the JSON yourself manually, build an array of the keys, and then use those keys to fetch the items from the dictionary in that order.
If the order of your data is important you should put it into an Array, not a Dictionary (which is inherently without order but with quick keyed access as others have noticed). Please change your backend accordingly and ask another question if it does not work.

JSON Reference to non-object value

We are currently investigating JSON as a potential API data transfer language for our system and a question about using JSON Reference came up.
Consider the following example:
{
"invoice-address" : { "street": "John Street", "zip": "12345", "city": "Someville" },
"shipping-address": { "$ref": "#/invoice-address" }
}
According to our research, this is a valid usage of JSON Reference. We replace the instance of an object with another object containing the reference pointing to a different object using a JSON Pointer fragment.
Now, a JSON Reference always consists of a key-value pair and thus has to be enclosed in an object. This would mean that in order to reference a non-object data type (e.g. the zip and city strings in the example above) you would have to do the following:
{
"invoice-address" : { "street": "John Street", "zip": "12345", "city": "Someville" },
"shipping-address": { "street": "Doe Street", "zip": { "$ref": "#/invoice-address/zip" }, "city": { "$ref": "#/invoice-address/city" } }
}
Even though the JSON Pointers now correctly point to string values, we had to change the data type of zip and city from string to object, which make them fail validation against our JSON Schema, because it declares them as strings.
However, the JSON Reference draft states:
Implementations MAY choose to replace the reference with the referenced value.
Does that mean that we are allowed to "preprocess" the file and replace the JSON Reference object with the resolved string value before validating against the JSON Schema? Or are references limited to object types only?
Thanks to anyone who can shed some light onto this.
I wouldn't expect most validators to resolve JSON References before validation. You could either:
resolve JSON References before validation
adapt the JSON Schemas to allow for JSON Reference objects in certain places
Personally, I think the first option is much neater.
You could end up with circular references I suppose - I don't know which validator/language you're using, but tv4 can definitely handle it.

Storing a JSON in a data-attribute, the correct way

I have a JSON:
person = {
"name": "john",
"surname": "smith",
"mobile": 123
}
And I want to store it in a data-attribute.
I'm achieving this, this way:
$(".someElement").data("jsonperson", escape(JSON.stringify(person)));
And when reading that data attribute:
JSON.parse(unescape($(".someElement").data("jsonperson")));
Is this the cleaner way? I'm talking about the escape and unescape.
Just do:
$(".someElement").data("jsonperson", person);
According to the documentation, the value parameter
can be any Javascript type including Array or Object.