Attribute as name context broker - fiware

I'm working with ContextBroker by inserting the data I retrieve from an API.
My problem is the following:
These attribute names are numeric and CB behaves strangely for example when I create this:
{
"id": "pruebas999",
"type": "typopruebas99",
"2000": {
"type": "float",
"value": 2.4,
"metadata": {
"timestamp": {
"type": "Integer",
"value": 1550804920
}
}
}
}
The information returns it in the following way, everything above the id and the type this is normal?
{
"2000": {
"type": "float",
"value": 2.4,
"metadata": {
"timestamp": {
"type": "Integer",
"value": 1550804920
}
}
},
"id": "pruebas999",
"type": "typopruebas99"
}
On the other hand to be numerical values I do not know the name of those sensors I have thought to create another entity with the name of the sensors and make reference in it to each id then you can retrieve the information of that entity with the value of each sensor and the first name? as an aggregation?
Greetings and thank you.

you should use an ISO8601 DateTime for your timestamps
"metadata": {
"timestamp": {
"type": "DateTime",
"value": "2019-12-12T12:00:00Z"
}
}

Related

How to model complex schema without much repetition using JsonSchema?

I am trying to model documents (such as passport) and their attributes (first name, birth date, etc) and describe them using json schema.
I want to define many types of documents in runtime, every document as a separate schema.
Some attributes may be referenced from multiple documents, such as firstName in passport and in driver's license, so it is
probably wise to create a separate attribute definition schema for this.
Lets say, I have a passport, its model looks like following:
{
"owner": "<string>",
"type" : "<string>",
"salt" : <number>,
"attributes": {
"number" : {"value": "<string>", "salt": <number>}
"lastName" : {"value": "<string>", "salt": <number>},
"firstName" : {"value": "<string>", "salt": <number>},
"birthDate" : {"value": "<date>", "salt": <number>},
"nationality": {"value": "<string>", "salt": <number>},
}
}
Every attribute has "value" and "salt", but "value" is different for every key (attribute name).
My question is, how will schema for this type of JSON document look?
UPD: every attribute has value and salt. Salt is exactly equal for all attributes, but value is different. If I manually specify that every attribute has salt and value, then I will end up with huge schema duplication. Is it possible to avoid it?
There are plenty of things you can do to reduce the duplication in your schema. One way is to use the definitions keyword. Another option is to use patternProperties to validate common constraints across multiple properties.
{
"type": "object",
"properties": {
"owner": { "type": "string" },
"type": { "type": "string" },
"salt": { "$ref": "#/definitions/salt" },
"attributes": {
"type": "object",
"patternProperties": {
".*": {
"type": "object",
"properties": {
"salt": { "$ref": "#/definitions/salt" }
},
"required": ["value", "salt"]
}
},
"properties": {
"number": { "$ref": "#/definitions/string-value" },
"lastName": { "$ref": "#/definitions/string-value" },
"firstName": { "$ref": "#/definitions/string-value" },
"birthDate": { "$ref": "#/definitions/date-value" },
"nationality": { "$ref": "#/definitions/string-value" }
}
}
},
"definitions": {
"salt": { "const": 1234 },
"string-value": {
"properties": {
"value": { "type": "string" }
}
},
"date-value": {
"properties": {
"value": { "type": "string", "format": "date" }
}
}
}
}
With draft-7 of JSON Schema (current at time of writing), assuming you'll want to be using references to another file which contains your property definitions, you may want to do something like the following.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"number": {
"properties": {
"value": {...}, # Whatever you define as your schema for number/value.
"salt": {
"$ref": "externalRef/definitions/salt"
}
}
}
}
}

Azure Data Factory json dataset missing property in typeProperties

I am creating a dataset from json-formatted data in an Azure Data Factory (v1). When using the following code, I get a Property expected error with the infotext Property specific to this data set type on the typeProperties object. From what I can see, I am using the same properties as in the example docs. What property am I missing?
Dataset definition:
{
"name": "JsonDataSetData",
"properties": {
"type": "AzureDataLakeStore",
"linkedServiceName": "TestAzureDataLakeStoreLinkedService",
"structure": [
{
"name": "timestamp",
"type": "String"
},
{
"name": "value",
"type": "Double"
}
],
"typeProperties": {
"folderPath": "root_folder/sub_folder",
"format": {
"type": "JsonFormat",
"filePattern": "setOfObjects",
"jsonPathDefinition": {
"spid": "$.timestamp",
"value": "$.value"
}
},
},
"availability": {
"frequency": "Day",
"interval": 1
}
}
}
Have you tried adding encodingName and nestingSeparator with the default values? The documentation has mistakes sometimes and a property that is documented as not required might be giving you this error.

Use object property keys as enum in JSON schema

I'm trying to validate a JSON file using JSON Schema, in order to find cases of "broken references". Essentially my file consists of items and groups, with each item belonging to a single group referenced by the groups property key, like so:
{
"items": {
"banana": {
"name": "Banana",
"group": "fruits"
},
"apple": {
"name": "Apple",
"group": "fruits"
},
"carrot": {
"name": "Carrot",
"group": "vegetables"
},
"potato": {
"name": "Potato",
"group": "vegetables"
},
"cheese": {
"name": "Cheese",
"group": "dairy"
}
},
"groups": {
"fruits": {
"name": "Fruits"
},
"vegetables": {
"name": "Vegetables"
}
}
}
In the example above the item cheese is to be considered invalid, as there are no dairy property in the groups object. I've tried to validate this using the following schema:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"title": "Food",
"id": "food",
"type": "object",
"properties": {
"items": {
"type": "object",
"patternProperties": {
"^[A-Za-z0-9-_.:=]+$": {
"properties": {
"name": {
"type": "string",
"pattern": "^[A-Za-z- ]+$"
},
"group": {
"pattern": "^[a-z]+$",
"enum": {
"$data": "/groups"
}
}
}
}
}
},
"groups": {
"type": "object",
"patternProperties": {
"^[A-Za-z0-9-_]+$": {
"properties": {
"name": {
"type": "string",
"pattern": "^[A-Za-z- ]+$"
}
}
}
}
}
},
"additionalProperties": false
}
This has the effect that the enum for group is populated by the property values in groups, but what I want to do is use the property keys defined in groups.
If I add a property like e.g. groupIds and let that be an array of all property keys found in groups and specify the enum as "$data": "/groupIds" it does work, so I take this to be a JSON pointer issue.
The enum keyword in JSON Schema is defined as:
The value of this keyword MUST be an array. This array SHOULD have at least one element. Elements in the array SHOULD be unique.
So if I could only get JSON pointer to reference an object's keys rather than its values I guess the enum validation would just work. I'm thinking something like "$data": "/groups/.keys", "$data": "/groups/$keys" or similar, but haven't found it while googling or reading the spec. Is there such a thing or has it ever been proposed?
There is no such thing. It’s very close to general expressions inside JSON and it may have some use cases, but there is no such specification.

Json schema limit number of the same object

Is there a way, to create a json schema, that limits the number a object can occur?
Or make sure, that a object is unique?
In the end, i want something like
{
"days": {
"monday": {
"schedule": {
"start_time": "23:35"
}
},
"tuesday": {
"schedule": {
"start_time": "23:23"
}
}
}
}
In this json, each day should only occur once. Like one start time per day.
So far, i tried it with the following schema but not successful at all.
With this schema, i can have multiple "monday" objects in my json and the schema is still valid. But what i'm looking for, is a schema that is not valid for more than one object.
{
"$schema": "http://json-schema.org/schema#",
"title": "DayScheduler",
"type": "object",
"required": [
"days"
],
"properties": {
"days": {
"monday": {
"$ref": "#/definitions/scheduler"
},
"tuesday": {
"$ref": "#/definitions/scheduler"
},
"wednesday": {
"$ref": "#/definitions/scheduler"
},
"thursday": {
"$ref": "#/definitions/scheduler"
},
"friday": {
"$ref": "#/definitions/scheduler"
},
"saturday": {
"$ref": "#/definitions/scheduler"
},
"sunday": {
"$ref": "#/definitions/scheduler"
}
}
},
"definitions": {
"scheduler": {
"type": "object",
"required": [
"schedule"
],
"properties": {
"schedule": {
"type": "object",
"required": [
"start_time"
],
"properties": {
"start_time": {
"type": "string",
"pattern": "^([01]?[0-9]|2[0-3]):[0-5][0-9]"
}
}
}
}
}
}
}
Is there a way to achieve this with a json-schema?
Duplicate keys are not allowed in JSON. JSON Schema is designed to validate JSON data. It's not equipped to validate data that is not valid JSON.
This example helps illustrate why this is not valid.
var value = {
"foo": 1,
"foo": 2
}
If you evaluate this as a JavaScript object and try to access the foo property of this object: value.foo === ???, is it 1 or 2? It's ambiguous.
My experience in JSON has lead me to understand that if you have multiple objects with the same key, the value of the last one will be taken for the key.
You could try adding
{
"name": name,
type: object,
properties: {
a:b,
c:d
},
"additionalProperties": false
}
to your schema, but I still think the last occurrence of each key will be taken as the value.

JSON schema verification

I have to create a form using JSON.
So as a first step i need to verify JSON with schema.
Here is a part of my JSON
"elements":{
"textbox":{
"name":{
"type":"text",
"name":"textbox",
"label":"Enter Your Name",
"required":false,
"disabled":false,
"maxlength":"",
"pattern":"",
"readonly":false,
"value":"",
"autocomplete":"off"
},
"school":{
"type":"text",
"name":"textbox",
"label":"F",
"required":false,
"disabled":false,
"maxlength":"",
"pattern":"",
"readonly":false,
"value":"",
"autocomplete":"off"
}
...
...
...
}
So inside "elements", it has a textbox, and one who types in the JSON can give any number of textbox field inside "textbox" for the form creation.
I need to write a JSON Schema to verify the data i.e, specifically i need to know how to do for this particular elements part. To define it as an array inside array or object..?? :( :/
Well, I do suggest that you define textbox as an array. This way, you could set different parameters for the objects in your array and then you would be able to verify the data this way.
Here is a little example of what I am talking about:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {},
"id": "example",
"properties": {
"elements": {
"id": "/properties/elements",
"properties": {
"textbox": {
"id": "/properties/elements/properties/textbox",
"items": {
"id": "/properties/elements/properties/textbox/items",
"properties": {
"Parameter1": {
"id": "/properties/elements/properties/textbox/items/properties/Parameter1",
"type": "string"
},
"Parameter2": {
"id": "/properties/elements/properties/textbox/items/properties/Parameter2",
"type": "number"
},
"Parameter3": {
"id": "/properties/elements/properties/textbox/items/properties/Parameter3",
"type": "integer"
}
},
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
},
"type": "object"
}
This way, the user can input as many textboxes he wants and you can still use the same schema to verify the JSON.