what's the property of a FeatureCollection in JSON - json

I am new to JSON and I am trying to understand it. So I want to specify a rectangle. Therefor I am using a FeatureCollection in JSON. According to this example I have this:
{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[ [0.0, 0.0], [5.0, 0.0], [5.0, 5.0],
[0.0, 5.0], [0.0, 0.0] ]
]
},
"properties": {
"prop0": "value0",
"prop1": {"this": "that"}
}
}
However, I am not sure what to set as a property?
I am assuming prop0 could be something like a name, but what is prop1? Something like type number or coordinates again?
"properties": {
"name": "null island",
"prop1": {"type": "number"}
}

You seem to be referring to GeoJSON. From the documentation:
"A GeoJSON object with the type "Feature" is a feature object.
A feature object must have a member with the name "geometry". The value of the geometry member is a geometry object as defined above or a JSON null value.
A feature object must have a member with the name "properties". The value of the properties member is an object (any JSON object or a JSON null value).
If a feature has a commonly used identifier, that identifier should be included as a member of the feature object with the name "id". "
In JSON, values are arrays, objects (a collection of key-value pairs, often referred to as dictionaries, maps, hashes, associative arrays), strings, numbers, boolean values, and null values.
The "properties" key can have either "null" as its value, or an object (dictionary). Each value in the dictionary can be anything, including another dictionary.

Related

Valid string sequences in JSON Schema

Can anyone advise how to code up a JSON Schema document to describe a string that can be one of three possible sequences? Say a string "fruit" can be only the following: "apple", "bananna" or "coconut".
I was thinking it might be possible to use regex but not sure how to indicate the regex constraint in JSON Schema.
https://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.6.1
Here is what I have so far:
{
"$id": "https://example.com/person.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "TestSession",
"type": "object",
"properties": {
"fruit": {
"type": "string",
"description": "only three legal possibilities: apple, banana, or coconut"
}
}
You need to use the enum keyword for this.
The value of this keyword MUST be an array. This array SHOULD have
at least one element. Elements in the array SHOULD be unique.
An instance validates successfully against this keyword if its
value is equal to one of the elements in this keyword's array
value.
Elements in the array might be of any type, including null.
https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#section-6.1.2
For example "enum": [ "apple", "bananna", "coconut" ].

filtering geoJSON data using JMESPath not working

I am trying to filter some data from the geoJSON data structure shown as below:
"features": [
{
"type": "Feature",
"properties": {
"#id": "node/7071544593",
"addr:city": "Joensuu",
"addr:housenumber": "12",
"addr:postcode": "80100",
"addr:street": "Siltakatu",
"addr:unit": "C 33",
"alt_name": "Crasman Oy Joensuu",
"alt_name_1": "Crasman Oy",
"name": "Crasman Joensuu",
"short_name": "Crasman",
"website": "https://www.crasman.fi"
},
"geometry": {
"type": "Point",
"coordinates": [
29.7621398,
62.6015236
]
},
"id": "node/7071544593"
},
{
"type": "Feature",
"properties": {
"#id": "node/7117872562",
"amenity": "car_rental",
"operator": "avis"
},
"geometry": {
"type": "Point",
"coordinates": [
29.7630643,
62.6036656
]
},
"id": "node/7117872562"
}
]
What I am trying to do is iterate through this array of features, look into the properties object to check if it contains website, if Yes, then I can print its coordinates from geometry object.
This is what I tried:
Features[*].properties[?contains(#,'website')=='true'].geometry.coordinates
It gives me null value
Try this:
features[?contains(keys(properties),'website')].geometry.coordinates
E.g.:
$ jp "features[?contains(keys(properties),'website')].geometry.coordinates" <input.json
[
[
29.7621398,
62.6015236
]
]
With regard to why your example didn't work:
Identifiers are case-sensitive, so you need features, not Features.
properties is an object, not an array, so you can't apply a filter expression to it.
Even if you could, it's not properties that you want to filter. You are trying to filter whole features.
contains tests if an array contains an item (or if a string contains a substring), not whether an object has a key. You can use keys() to get the keys of an object in an array.
You don't need to compare the result of contains() to true, it's already a boolean.
Even if you were trying to compare to true, you'd need to use backticks: `true`, not quotes 'true'.

JSON schema for key (unknown column name): value (list of integers) pairs

I want to make JSON schema for JSON which looks something like that (It's for constructing regressors with delays.):
{'x1': [1,6,2], 'col5': [0], 'y': [1, 6, 3, 8]}
I don't know column names and neither the length of the lists in advance. The only thing I know is that column name should be a string and list of values an array. Any advice how to construct it?
I'm open to more suitable JSON format and it's scheme.
Although is is possible to achieve with patternProperties of .* pattern the more straightforward way is to use additionalProperties schema attribute, e.g.:
{
"type": "object",
"additionalProperties": {
"type": "array",
"items": {
"type": "integer"
}
}
}
In this example I also restricted array element type to integer.
This sounds like a perfect use-case for JSON Schema. It allows you add as few or as many constraints as are are needed. The following schema requires that the JSON be an object where all properties must be an array. An array of what? It could be anything. It's unconstrained.
{
"type": "object",
"patternProperties": {
".*": { "type": "array" }
}
}

Is it possible to write a generic JSON Schema?

Inside my root JSON object I have many JSON objects of two different types. I'm wondering if there is a way to write a JSON schema to validate these objects without getting specific, i.e. generic schema.
For example, imagine I have the following JSON:
"Profile":
{
"Name":
{
"Type": "String",
"Value": "Mike",
"Default": "Sarah",
"Description": "This is the name of my person."
}
"Age":
{
"Type": "Number",
"Value": 27,
"Default": 18,
"Description": "This is the age of my person."
}
}
This Profile JSON object represents a collection of various details about a person. Notice I have two different types of inner Objects, String Objects and Number Objects. Taking this into account, I would now like to create a JSON Schema to validate any of the inner objects without being specifc about which Objects they are, e.g. I don't care that we have "Name" or "Age", I care that we have proper String Objects and Number Objects.
Does JSON Schema give me the ability to do this? How do I write a generic JSON Schema based on the kinds of Objects I have and not specific object names?
Here is what I've got so far:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"StringObject": {
"type": "object",
"properties": {
"Type": {
"type": "string"
},
"Value": {
"type": "string"
},
"Default": {
"type": "string"
},
"Description": {
"type": "string"
}
},
"required": [
"Type",
"Value",
"Default",
"Description"
]
}
}
}
Inside my root JSON object I have many JSON objects of two different types. I'm wondering if there is a way to write a JSON schema to validate these objects without getting specific, i.e. generic schema.
Union types are defined to handle this:
A value of the "union" type is encoded as the value of any of the member types.
Union type definition - An array with two or more items which indicates a union of type definitions. Each item in the array may be a simple type definition or a schema.
{
"type":
["string","number"]
}
References
JSON Encoding of Data Modeled with YANG: 6.10. The "union" Type
A JSON Media Type for Describing the Structure and Meaning of JSON Documents: Union Types

Modelling GeoJSON in Swagger

I'm trying to add a definition for GeoJSON in a Swagger API that serves JSON. I'm running into some issues with defining the options that GeoJSON has for features.
A single feature can be a choice of "Point", "MultiPoint", "LineString", "MultiLineString", "Polygon", or a "MultiPolygon". Each of these though imposes different constraints on their coordinates field. For instance, a point might have a single coordinate consisting of an longtitude latitude pai, while a Polygon might have a field that looks like this:
[ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ].
I've looked around but there seems no elegant solution to this problem except by just defining the geometry as a string instead of a complex object like this. This means a loss of information, and makes it undesirable to port APIs based on a Swagger specification because input strings will have to be parsed to an object first.
The GeoJSON objects look like this.
What I've got so far is this:
"Feature": {
"type": "object",
"properties": {
"id": {
},
"geometry": {
"$ref": "#/definitions/Geometry"
},
"type" : {
"type" : "string"
},
"properties": {
"type": "object"
}
}
}
And the geometry definition looks like this:
"Geometry": {
"type": "object",
"properties": {
"type":{
"type": "string",
"enum": ["Point","MultiPoint","LineString","MultiLineString","Polygon","MultiPolygon"]
},
"coordinates": {
"type": "array",
"items": {
"type": "number",
"example": [4.49965, 52.06891]
}
}
}
}
Have you checked this gist file: geometry_geojson-yaml
I'm afraid there's no way to describe that using Swagger. You can describe a single type, but not something that would accept/produce all types.
It seems to me that the most recent attempt is this one: https://app.swaggerhub.com/apis/OlivierMartineau/GeoJSON/1.0.1
It was created by an "OlivierMartineau" aka "zitoun" or "zit0un" who used the already mentioned (by #jmini) "gist" by "bubbobne" as well as one of the forks by "idkw" as the foundation for his model.