Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JsonSchema Library to verify JSON data structure in python

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to use the JsonSchema library to verify the JSON data structure in python", the editor shows you the operation process through an actual case, the method of operation is simple and fast, and practical. I hope that this article "how to use the JsonSchema library to verify the JSON data structure in python" can help you solve the problem.

Simple example

There is a simple json data that writes jsonschema according to the json data format, and then verifies that each field of the json data is of a specified type.

Import jsonschemajson_data = [{'pm10': 24,' city': 'Zhuhai', 'time':' 2016-10-23 1313 city': 0000'}, {'pm10': 24,' city': 'Shenzhen', 'time':' 2016-10-21 13 city': 0015'}, {'pm10':' 21, 'Guangzhou' Json_schema = {'type':' array', 'items': {' type': 'object',' properties': {'pm10': {' type': 'number',} 'city': {' type': 'string',' enum': ['Zhuhai', 'Shenzhen']}, 'time': {' type': 'string'} try: jsonschema.validate (json_data Json_schema) except jsonschema.ValidationError as ex: msg = ex print (ex) type keyword

The type keyword is the basis of the json schema, specifying the data type of the schema. The core of JSON Schema defines the following basic types:

String

Numeric types

Object

Array

Boolean

Null

The corresponding types of these types in Python are as follows, and the following table maps the names of JavaScript types to related types of Python:

JavaScriptPythonstringstringnumberint/floatobjectdictarraylistbooleanboolnullnone

The type keyword can be a string or an array:

If it is a string, then it is the name of the above basic energy type

If it is an array, it must be an array of strings, where each string is the name of one of the basic types, and each element is unique. In this case, if the json snippet matches any given type, the change is valid.

Here is a simple example of the type keyword:

{"type": "number"}

Define a field type as number. If it is 4043.0, this is verified. If it is "43", a string containing a number cannot be verified.

{"type": ["number", 'string']}

If a field type is defined as number or string, if it is 43, or "me and you" is checked, if it is [43, "me and you"], it will not pass because structured data types are not accepted.

Object keyword

In Python, the corresponding type of an object is the dict type.

Property properties

Use the properties keyword to define properties (key-value pairs) on an object. For example, we want to define a simple pattern for addresses made up of numbers, street names, and street types

{"type": "object", "properties": {"number": {"type": "number"}, "street_name": {"type": "string"}, "street_type": {"type": "string", "enum": ["Street" "Avenue", "Boulevard"]} required properties

By default, properties does not require properties defined by keywords. However, you can use the required keyword to provide a list of desired attributes.

The required keyword accepts an array of one or more strings. Each string must be unique.

In the following example pattern that defines user records, we require each user to have a name and email address, but we don't mind if they provide their address or phone number:

{"type": "object", "properties": {"name": {"type": "string"}, "email": {"type": "string"}, "address": {"type": "string"}, "telephone": {"type": "string"}} "required": ["name", "email"]} size

You can use the minProperties and maxProperties keywords to limit the number of attributes on an object. Each of these must be a non-negative integer.

{"type": "object", "minProperties": 2, "maxProperties": 3} array attributes

Arrays are used for ordered elements.

In Python, array is similar to the list or tuple type, depending on usage.

For example: [1, 2, 3, 4, 5]

[2, 'dd']

Items

The elements of an array can be anything, but it is often useful to validate the items of an array against some patterns. This is done using the items and additionalItems keywords.

In JSON, data is usually used in two ways

List validation: a sequence of arbitrary length in which each element matches the same pattern

Tuple validation: a fixed-length sequence in which each item may have a different pattern, in which the index (location) of each item is meaningful for how to interpret the value, such as Python's tuple.

List validation

List validation is useful for arrays of any length, where each item matches the same pattern, for which the items keyword is set to a single pattern, which is used to validate all items in the array.

Note: items is a single mode at this time, and the additionalItems keyword is meaningless and should not be used.

For example, in the following example, we define that each item in the array is a number.

{"type": "array", "items": {"type": "number"}}

If it is [1 pass 2 2 4 4 5],

If it is [1, 2, 3, 5, 6], false

If it is [], pass

Tuple validation

Tuple validation is required when the array is a collection of items. Each of these projects has a different schema, and the ordinal index of each project makes sense.

For example, the street address is expressed in this way

1600 Pennsylvania Avenue NW has 4 type [number, streent_name, street_type, direction]

Each field has a different schema

Number: address number, must be numeric

Street_name: the name of the street, which must be a string

Street_type: the type of street, which should be a set of fixed-value strings

Direction: the location of the address, which should be a string from a different value set

For this reason, we set the items keyword to an array, where each item is a pattern corresponding to each index of the document array, that is, an array, and the first element pattern validates the first element of the input array. The second element schema validates the second element of the input array, and so on

Example:

{"type": "array", "items": [{"type": "number"}, {"type": "string"}, {"type": "string", "enum": ["Street", "Avenue" "Boulevard"]}, {"type": "string", "enum": ["NW", "NE", "SW", "SE"]}]}

If it is [1600, "Pennsylvania", "Street'," NW "], if pass is [10 Street',', 'etc.], false and by default, you can add other items: [1600," Pennsylvania "," Street "," NW "," Washington "]

The additionalItems keyword controls whether there are other items that exceed the array defined in the schema, and if set to false, additional items in the array are not allowed.

Length

You can use the minItems and maxItems keywords to specify the length of the array. The value of each keyword must be non-negative. These keywords are valid for both List and Tuple validation. Example:

{"type": "array", "minItems": 2, "maxItems": 3} uniqueness

Using the uniqueItems keyword set to true, each item in the array is unique.

Common keyword metadata

The json schema contains several keywords, title,description and default, which are not strictly used to validate the format, but are used to describe part of the schema. In title and description butler you must be a string

Enumerated value

The enum keyword is used to restrict values to a fixed set of values, which must be an array that must contain an element, each of which is unique.

{'type':' string', 'enum': [' red', 'green']}

If the value of the verification field is passed in the enumeration, if not cannot be verified.

Combination mode

JSON Schema contains keywords for grouping patterns together, which does not mean combining patterns from multiple files or JSON trees, although these tools help to achieve this and are described in structured complex patterns.

For example, in the following pattern, the anyOf keyword is used to indicate that a given value may be valid for any given subpattern. The first subpattern requires a string with a maximum length of 5. The second subpattern requires a number with a minimum value of 0. As long as a value validates any of these patterns, it is considered valid as a whole.

{'anyOf': [{' type':'string', 'maxLength': 5}, {' type':'string', 'minimum': 0}]}

The keywords for the combination mode are:

AllOf: must be valid for all submodes

AnyOf: must be valid for any submode (one or more)

OneOf: must be valid for only one of the submodes

AnyOf

To validate anyOf, the given data must be valid for any (one or more) given subschemas.

{"anyOf": [{"type": "string"}, {"type": "number"}]}

If it is "Hello", if pass is 33, if pass is ['ddd', 33], false

OneOf

To validate oneOf, the given data must be valid for only one of the given subschemas.

{"oneOf": [{"type": "number", "multipleOf": 5}, {"type": "number", "multipleOf": 3}]}

If it is a multiple of 5, if pass is a multiple of 3, if pass is a multiple of 5 and 3, false

AllOf

To validate allOf, the given data must be valid for all given subschemas.

{"allOf": [{"type": "string"}, {"maxLength": 5}]} $schema keyword

The $schema keyword is used to declare that the JSON fragment is actually part of the JSON schema. It also declares which version of the JSON Schema standard is written for the pattern.

It is recommended that all JSON schemas have a $schema entry, which must be located in the root directory. Therefore, in most cases, you need to be in the root directory of the schema:

"$schema": "http://json-schema.org/schema#"

Regular expression

The pattern and pattern attribute keywords use regular expressions to represent constraints. The regular expression syntax used is from JavaScript (specifically ECMA 262). However, this complete syntax is not widely supported, so it is recommended that you stick to a subset of the following syntax.

A single unicode character (except for the special characters below) matches itself.

^: matches only the beginning of the string.

$: matches only the end of the string.

(...) Groups a series of regular expressions into a single regular expression

|: match | regular expression before or after the symbol.

[abc]: match any character in brackets.

[amurz]: matches the range of characters.

[^ abc]: matches any characters not listed.

[^ aMurz]: matches any character outside the range.

+: matches one or more duplicates of the previous regular expression.

*: matches zero or more repetitions of the previous regular expression.

Matches the zero or one repetition of the previous regular expression.

+ + and? Qualifiers are greedy; they match as much text as possible. Sometimes this behavior is undesirable, and you want to match as few characters as possible.

{x}: exact x matches the number of occurrences of the previous regular expression.

{xrecoery y}: matches at least x and the most y of the previous regular expressions appear multiple times.

{x,}: matches the number of times regular expressions appear before x or more.

{x}?, {xQuery}?, {x,}?: the inert version of the above expression.

Example:

{"type": "string", "pattern": "^ (\ ([0-9] {3}\))? [0-9] {3}-[0-9] {4} $"}

If it is "555-1212", if pass is "(888) 555-1212", if pass is "(888) 555-1212 extension 532", false

Building complex pattern reuse

Some patterns may be common in several places. Rewriting each time will make the pattern more verbose, and later updates will be very complex, which we can do in a way of reuse. For example: define customer records, each customer may have both a shipping address and a billing address, always the same address, street address, city, state name.

Define the address pattern:

{"type": "object", "properties": {"street_address": {"type": "string"}, "city": {"type": "string"}, "state": {"type": "string"}}, "required": ["street_address", "city", "state"]}

We reuse the above pattern and put it under the parent schema, called definitions:

{"definitions": {"address": {"type": "object", "properties": {"street_address": {"type": "string"}, "city": {"type": "string"} "state": {"type": "string"}, "required": ["street_address", "city", "state"]}

Then we use the $ref keyword to reference this schema fragment from elsewhere, pointing to the location of the module

{"$ref": "# / definitions / address"}

The value $ref is a string in the format called JSON Pointer.

'#' refers to the current document, and / 'traverses the keys in the objects in the document, so

"# / definitions / address" means:

Go to the root of the document

Find the value "definitions" of the secret key

In this object, find the value "address" of the key

$ref can also be a relative or absolute URI, for example:

{"$ref": "definitions.json#/ address"}

This is the end of the introduction to "how to use the JsonSchema library to verify JSON data structures in python". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report