I have the following JSON format and want to create a mapping for it from Elasticsearch console :
{
"properties": {
"#timestamp" : {
"type" : "date"
},
"list": [
{
"name": "John",
"age": "37",
"title": "Tester"
}
]
}
}
There's no list or array type in ES, you simply declare objects and then you can add a list of those objects to your documents:
PUT your-index
{
"mappings": {
"properties": {
"#timestamp" : {
"type" : "date"
},
"list": {
"type": "object",
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "integer"
},
"title": {
"type": "text"
}
}
}
}
}
}
Related
How to create avro schema for below json code
{
"_id" : "xxxx",
"name" : "xyz",
"data" : {
"abc" : {
"0001" : "gha",
"0002" : "bha"
}
}
}
Here,
"0001" : "gha",
"0002" : "bha"
key: value would be dynamic.
Maybe a schema like this does what you want?
{
"type": "record",
"name": "MySchema",
"namespace": "my.name.space",
"fields": [
{
"name": "_id",
"type": "string"
},
{
"name": "name",
"type": "string"
},
{
"name": "data",
"type": {
"type": "record",
"name": "Data",
"fields": [
{
"name": "abc",
"type": {
"type": "map",
"values": "string"
}
}
]
}
}
]
}
It's not dynamic, but you can add as many key-value pairs to the map as you like. Field names starting with a numeric value aren't allowed in Avro.
I have a json schema as follows
{
"$id": "https://example.com/arrays.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "A representation of a person, company, organization, or place",
"type": "object",
"properties": {
"fruits": {
"type": "array",
"items": {
"type": "string"
}
},
"vegetables": {
"type": "array",
"items": { "$ref": "#/$defs/veggie" }
}
},
"required" : ["fruits", "vegetables"],
"$defs": {
"veggie": {
"type": "object",
"required": [ "veggieName", "veggieLike", "cropLocation"],
"properties": {
"veggieName": {
"type": "string",
},
"veggieLike": {
"type": "boolean",
},
"cropLocation" : {
"type" : "object",
"items" :{ "$ref" : "#/$defs/location"}
}
}
},
"location" : {
"type" : "object",
"required" : ["country", "state"],
"properties" : {
"country" : {
"type": "string"
},
"state" : {
"type": "string"
}
}
}
}
}
When I give data as follows I am expecting error that cropLocation doesnt have state and country prperty. but it validates as success against that schema. How to define schema with multilevel complex objects.
"fruits": [ "apple", "orange", "pear" ],
"vegetables": [
{
"veggieName" : "carrot",
"veggieLike": true,
"cropLocation" : {}
},
{
"veggieName": "broccoli",
"veggieLike": false,
"cropLocation" : {}
}
]
}
I tried variuos ways to restrucutre the json schema, but is not working
The error is here:
"items" :{ "$ref" : "#/$defs/location"}
"cropLocation" is an object, but items is a keyword that only applies to arrays. Simply remove the items keyword and make the $ref a sibling to "type": "object".
I'd like to reference and factor the following json schema in multiple files for a better handling regarding its properties. Files as "$ref": "book.json" and "$ref": "pencil.json" which each file has its own properties and validations (and of course, that it works "addProperties = false"), I have the following schema:
{
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
},
"type": {
"type": "string",
"enum": ["Pencil", "Book"]
},
"title": {
"type": "string"
},
"content": {
"type": "string"
},
"domain": {
"type": "string"
},
"img_url": {
"type": "string"
},
"url": {
"type": "string"
}
},
"allOf": [
{
"if": {
"properties": {
"type": {
"enum": ["Book"]
}
}
},
"then": {
"required": [
"domain", "img_url", "url"
],
"not" : {
"anyOf" : [
{ "required" : ["content"] }
]
}
}
},
{
"if": {
"properties": {
"type": {
"enum": ["Pencil"]
}
}
},
"then": {
"required": [
"content"
],
"not" : {
"anyOf" : [
{ "required" : ["domain"] },
{ "required" : ["img_url"] },
{ "required" : ["url"] }
]
}
}
}
]
}
and I would like something like this:
# book.json
{
"properties": {
"content": {
"type": "string"
}
},
"if": {
"properties": {
"type": {
"enum": ["Book"]
}
}
},
"then": {
"required": [
"content"
],
"not" : {
"anyOf" : [
{ "required" : ["domain"] },
{ "required" : ["img_url"] },
{ "required" : ["url"] }
]
}
}
}
# pencil.json
{
"properties":{
"domain": {
"type": "string"
},
"img_url": {
"type": "string"
},
"url": {
"type": "string"
}
},
"if": {
"properties": {
"type": {
"enum": ["Pencil"]
}
}
},
"then": {
"required": [
"domain", "img_url", "url"
],
"not" : {
"anyOf" : [
{ "required" : ["content"] }
]
}
}
}
# properties.json
{
"additionalProperties": false,
"properties": {
"$ref": book.json,
"$ref": pencil.json
}
I would like to simplify it, whether a property is of the book or pencil type then it refers to the file where this file has its properties and validations.
I have a field in our data with multiple typing:
It could be type=string, which has the schema:
{"mixed_field" : {"type":"string"} }
Other times it could be type=object, the schema looks like:
{"mixed_field" : {
"properties": {
"access_token": {
"type": "string"
},
"created_at": {
"type": "integer"
}
},
"type": "object"
}
}
How do I express that "mixed_field" can be either type string or type object? Should I use the "oneOf" keyword as follows?
{
"mixed_field": {
"oneOf": [
{
"type": "string"
},
{
"properties": {
"access_token": {
"type": "string"
},
"created_at": {
"type": "integer"
}
},
"type": "object"
}
]
}
}
You can use oneOf/anyOf or you can use "type": ["string", "object"], in case it is a string "properties" keyword will be ignored.
I have following JSON Payload where i am trying to add ENUM type Value for one of the element.
{
"firstName" : "firstName",
"lastName" : "lastName",
"systemIds" : [ {
"systemName" : "SAP",
"systemId" : "99c27c63-e0b6-4585-8675-7aa3811eb4c3"
}, {
"systemName" : "SFDC",
"systemId" : "b65abf1d-825d-4ee3-9791-02d2cdd5e6f4"
}, {
"systemName" : "MONGODB",
"systemId" : "18e50430-8589-42d6-8477-58839a8bf202"
} ]
}
And here is my Schema which i tried to modify after it was auto generated using this website. http://jsonschema.net/#/
I added manually ENUM types as per my expectation here. Please correct what's wrong with this SCHEMA.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://abcd.com/schemas/customerInfo",
"type": "object",
"properties": {
"firstName": {
"id": "http://abcd.com/schemas/customerInfo/firstName",
"type": "string"
},
"lastName": {
"id": "http://abcd.com/schemas/customerInfo/lastName",
"type": "string"
},
"systemIds": {
"id": "http://abcd.com/schemas/customerInfo/systemIds",
"type": "array",
"minItems": 1,
"uniqueItems": false,
"additionalItems": true,
"items": {
"anyOf": [
{
"id": "http://abcd.com/schemas/customerInfo/systemIds/0",
"type": "object",
"properties": {
"systemName": {
"id": "http://abcd.com/schemas/customerInfo/systemIds/0/systemName",
"type": { "enum": [ "SAP", "MONGODB", "ERP", "SFDC" ] }
},"required": ["type"],
"systemId": {
"id": "http://abcd.com/schemas/customerInfo/systemIds/0/systemId",
"type": "string"
}
}
}
]
}
}
}
}
The the schema for the array items doesn't look right.
{
"anyOf": [
{
"id": "http://abcd.com/schemas/customerInfo/systemIds/0",
"type": "object",
"properties": {
"systemName": {
"id": "http://abcd.com/schemas/customerInfo/systemIds/0/systemName",
"type": {
"enum": [
"SAP",
"MONGODB",
"ERP",
"SFDC"
]
}
},
"required": [
"type"
],
"systemId": {
"id": "http://abcd.com/schemas/customerInfo/systemIds/0/systemId",
"type": "string"
}
}
}
]
}
You stated that there should be a "required" property but you put in an invalid schema. That needs to be removed. But perhaps you meant that the "type" property is required somewhere and is misplaced. I don't see any relation.
The "systemName" property is a string type with values that should be within that enumeration. The schema there is invalid.
This should work for you:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://abcd.com/schemas/customerInfo",
"type": "object",
"properties": {
"firstName": {
"id": "http://abcd.com/schemas/customerInfo/firstName",
"type": "string"
},
"lastName": {
"id": "http://abcd.com/schemas/customerInfo/lastName",
"type": "string"
},
"systemIds": {
"id": "http://abcd.com/schemas/customerInfo/systemIds",
"type": "array",
"minItems": 1,
"uniqueItems": false,
"additionalItems": true,
"items": {
"anyOf": [
{
"id": "http://abcd.com/schemas/customerInfo/systemIds/0",
"type": "object",
"properties": {
"systemName": {
"id": "http://abcd.com/schemas/customerInfo/systemIds/0/systemName",
"type": "string",
"enum": [ "SAP", "MONGODB", "ERP", "SFDC" ]
},
"systemId": {
"id": "http://abcd.com/schemas/customerInfo/systemIds/0/systemId",
"type": "string"
}
}
}
]
}
}
}
}