Method for converting tree structure JSON data into SQL statement query condition
By defining a set of operators and database mapping rules, the system intelligently parses JSON data to generate SQL query conditions, solving the problems of low efficiency and high error rate in converting tree-structured JSON data into SQL query conditions, and achieving efficient and accurate data processing.
Patent Information
- Application Number
- CN202410057506.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-01-16
- Publication Date
- 2026-02-24
- Estimated Expiration
- 2044-01-16
AI Technical Summary
Existing technologies struggle to efficiently convert tree-structured JSON data into SQL query conditions suitable for various databases, and suffer from high error rates and low efficiency.
Define operator sets and database mapping rules, generate SQL query conditions by intelligently parsing JSON data, including data validation, parsing and assembling SQL statements, and is suitable for various database systems.
It improves the efficiency and accuracy of data processing, is applicable to the generation of query conditions for various databases, and reduces development costs and error rates.
Smart Images

Figure CN117971864B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of database query technology, specifically a method for converting tree-structured JSON data into SQL query conditions. Background Technology
[0002] In today's digital environment, tree-structured JSON (JavaScript Object Notation) data is a common data exchange format widely used in various applications, from web applications to mobile applications and big data analytics.
[0003] In existing technologies, the aforementioned data format is suitable for representing complex hierarchical data. However, during transmission, storage, and analysis, it often needs to be converted into SQL (Structured Query Language) query conditions for more efficient querying and analysis in relational databases. Converting tree-structured JSON data into SQL query conditions still presents some difficulties and challenges in practical applications. Traditional methods typically involve manually writing code to parse JSON data and convert it into SQL query statements, which is not only time-consuming but also error-prone. Due to the hierarchical structure and complexity of tree-structured JSON data, developers need to be familiar with the data structure and write a large amount of mapping logic. Furthermore, as the data scale and complexity increase, manual conversion becomes even more difficult, potentially leading to errors and inefficiencies.
[0004] Another issue is the generality of the data. The structure of tree-structured JSON data can vary from application to application, with each application potentially having different hierarchical structures and attributes. Therefore, developing a general method to process tree-structured JSON data of different types and structures and transform it into query conditions applicable to various SQL databases is a challenging task. Existing technologies attempt to address these issues using data transformation tools, custom scripts, or manual coding. However, these methods may have limitations in terms of flexibility, efficiency, and accuracy. Therefore, there is a need for a more general and efficient method to automatically transform tree-structured JSON data into SQL query conditions applicable to various databases, thereby solving the aforementioned problems. Summary of the Invention
[0005] The purpose of this invention is to provide a method for converting tree-structured JSON data into SQL query conditions, so as to solve the problems mentioned in the background art.
[0006] To achieve the above objectives, the present invention provides the following technical solution: a method for converting tree-structured JSON data into SQL query conditions, the method comprising the following steps:
[0007] Define a set of operators;
[0008] Define database mapping rules;
[0009] Retrieve tree-structured JSON data;
[0010] Relevant data verification;
[0011] Intelligent JSON parsing;
[0012] Assemble SQL statements and execute database queries.
[0013] Preferably, the specific operations for defining an operator set include:
[0014] Users first need to customize a set of operators according to their own business needs, which will be used to construct SQL query conditions in the subsequent condition generation process.
[0015] Preferably, the specific operations for defining database mapping rules include:
[0016] Define database mapping rules to map attributes in the tree-structured JSON data to columns in the database table for use in subsequent condition generation and SQL statement construction.
[0017] Preferably, the specific operations for obtaining tree-structured JSON data include:
[0018] The front-end concatenates the JSON data into a tree structure and sends it to the back-end.
[0019] Preferably, the specific steps for verifying relevant data include:
[0020] Perform relevant validations on the tree-structured JSON data passed from the front end, including checking the validity of the JSON data, the correctness of the structure, and the accuracy of the data type.
[0021] Preferably, the specific operations for intelligent JSON parsing include:
[0022] Using a predefined set of operators and database mapping rules, the system intelligently parses tree-structured JSON data. It takes the root node as input, performs data validation, and determines whether the tree node data conforms to the structure. For example, if a node contains no data (an empty node), it returns null directly. Next, it checks if the node is a leaf node. If it is, it uses the operators in the node and the database mapping rules to assemble an SQL query condition statement by calling methods in the SQL class of the Hutool tool. Once assembled, the individual SQL query condition is returned.
[0023] Preferably, the specific operations for assembling SQL statements and executing database queries include:
[0024] Based on the parsed conditions and other parameters passed from the front end, the SQL statement is assembled, including constructing a SELECT statement, adding a FROM clause, applying connection operations, executing database query operations, and sending the SQL statement to the database system to retrieve data that meets the conditions.
[0025] Compared with the prior art, the beneficial effects of the present invention are:
[0026] This invention proposes a method for converting tree-structured JSON data into SQL query conditions, transforming it into SQL query conditions applicable to various databases. This method improves the efficiency and accuracy of data processing through parsing and condition generation, and is suitable for data analysis and database querying. Attached Figure Description
[0027] Figure 1 This is a flowchart of the method of the present invention;
[0028] Figure 2 This is a flowchart of the JSON parsing process of this invention. Detailed Implementation
[0029] To make the objectives, technical solutions, and advantages of the present invention clear and complete, the embodiments of the present invention will be further described in detail below with reference to the accompanying drawings. It should be understood that the specific embodiments described herein are only some, not all, embodiments of the present invention, and are merely illustrative of the embodiments of the present invention. They are not intended to limit the embodiments of the present invention. All other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0030] Example 1
[0031] Please see Figures 1 to 2 This invention provides a technical solution: a method for converting tree-structured JSON data into SQL query conditions, the method comprising the following steps:
[0032] (1) Define the set of operators:
[0033] In the embodiments of this patent, users first need to customize an operator set according to their own business needs. The operator set is a collection of logical operators, comparison operators, and concatenation operators, along with their corresponding query types, used to construct SQL query conditions during subsequent condition generation. For example, text data corresponds to equal to (“=") and fuzzy logic (“like”), numeric data corresponds to equal to, greater than, less than, etc. Logical operators (AND, OR) are generally used to concatenate conditions for various query types. These logical operators are typically placed in the branch nodes of a tree-structured JSON data structure for concatenating query conditions within their subtrees.
[0034] (2) Define database mapping rules:
[0035] In implementation, database mapping rules need to be defined. These rules map attributes in the tree-structured JSON data to columns in the database table for use in subsequent condition generation and SQL statement construction. For example, if the tree-structured JSON data has an attribute representing the product name, the mapping rule will map that attribute to the product name column in the database table.
[0036] (3) Obtain tree-structured JSON data:
[0037] This method primarily operates on the backend, requiring the frontend to concatenate the tree-structured JSON data and send it to the backend. Because tree-structured JSON data can adapt to various complex scenarios, the frontend offers multiple ways to represent it, such as visual selection and script writing, enhancing the flexibility of business performance.
[0038] (4) Validation of relevant data:
[0039] In this implementation, the tree-structured JSON data passed from the front end needs to be validated. This includes checking the validity of the JSON data, the correctness of its structure, and the accuracy of its data type. Ensuring that the input data meets the expected format and requirements is crucial to avoid errors in subsequent processing. Additionally, users can handle relevant permission issues according to their specific business needs.
[0040] (5) Intelligent JSON parsing:
[0041] This is a key step in this patent. In the implementation, a predefined set of operators and database mapping rules are used to intelligently parse the tree-structured JSON data. The parsing process is as follows: Figure 2As shown. First, the root node is passed in, and data validation is performed to determine if the tree node data conforms to the structure. For example, if a node has no data information, it is an empty node, and therefore does not meet the requirements and returns null directly. Second, it is determined whether the node is a leaf node. If it is a leaf node, the SQL query condition statement is assembled by calling the SQL class method in the Hutool tool according to the combination of operators in the node and the mapping rules of the database. After assembly, a single SQL query condition is returned. If the node is a branch node, an empty SQL query condition statement resultSQL is declared first. Then, all subtrees of the branch node are recursively parsed. After each recursive iteration, a temporary variable is used to receive its return value. The temporary variable storing the subtree query conditions is enclosed in parentheses and concatenated with the resultSQL statement. This concatenation requires the logical words (AND, OR) of the branch node. After all subtrees have been recursively parsed, resultSQL stores the SQL query conditions formed by the subtree rooted at the branch node. Finally, resultSQL is returned. Finally, after the recursion is complete, relevant data processing is performed, such as removing null values from the query conditions.
[0042] (6) Assemble the SQL statement and execute the database query:
[0043] Finally, based on the parsed conditions and other parameters passed from the front end, the SQL statement is assembled. This includes constructing the SELECT statement, adding the FROM clause, and applying join operations. Subsequently, the database query is executed, sending the SQL statement to the database system to retrieve data that meets the conditions.
[0044] Example 2
[0045] Building upon Example 1, this method achieves efficient conversion from JSON data to SQL query conditions through parsing and automatic mapping. The main steps and features of this method are as follows:
[0046] (1) Intelligent JSON Parsing: To solve the tedious problem of traditional manual conversion, this method adopts a highly efficient JSON parsing algorithm. This algorithm can accurately parse multi-level JSON objects and arrays, extracting data elements layer by layer from top to bottom and from left to right, and concatenating SQL query conditions during the data extraction process. In this way, no matter how tree-structured or complex the JSON data is, it can be effectively parsed.
[0047] (2) Condition Generation and Mapping: Based on general-purpose SQL statements, this method defaults to a set of condition statements that can intelligently generate conditions suitable for SQL queries. These conditions include logical operations (such as AND, OR), comparison operators (such as equal to, greater than, less than), and join operations (such as INNER JOIN, LEFT JOIN). For example, for text data, this method is designed to bind conditions such as equal to, fuzzy equal to, empty, and not empty. Users can directly select the corresponding query comparison operators on the front-end page. At the same time, this method also allows for custom query logic, enabling users to customize query logic that better suits their business needs. More importantly, this method utilizes predefined mapping rules to automatically map attributes in JSON data to columns in database tables. In this way, the generated query conditions can be accurately mapped to the database structure, achieving effective data transformation.
[0048] (3) Flexibility and versatility: This method is highly flexible and can adapt to different types and structures of tree-structured JSON data. Users can customize mapping rules according to application needs, thereby adapting to different data models and database systems. This versatility makes the method applicable to multiple fields and application scenarios.
[0049] (4) Improved Data Processing Efficiency: Compared to traditional manual conversion methods, this method can significantly improve data processing efficiency. Developers no longer need to spend a lot of time writing tedious mapping code; instead, they can use this method to automatically implement the conversion process. This helps reduce development costs, minimize errors, and improve the speed and accuracy of data processing.
[0050] Although embodiments of the invention have been shown and described, it will be understood by those skilled in the art that various changes, modifications, substitutions and alterations can be made to these embodiments without departing from the principles and spirit of the invention, the scope of which is defined by the appended claims and their equivalents.
Claims
1. A method for converting tree-structured JSON data into SQL query conditions, characterized in that: The method includes the following steps: Define a set of operators; Define database mapping rules; Retrieve tree-structured JSON data; Relevant data verification; Intelligent JSON parsing; Assemble SQL statements and execute database queries; The specific operations of intelligent JSON parsing include: using a predefined set of operators and database mapping rules to intelligently parse tree-structured JSON data; First, the root node is passed in, and data validation is performed to determine whether the tree node data conforms to the structure. If the node has no data information, it is an empty node, and therefore does not meet the requirements and returns null directly. Second, it is determined whether the node is a leaf node. If it is a leaf node, the SQL query condition statement is assembled by calling the SQL class in the Hutool tool according to the combination of operators in the node and the mapping rules of the database. After assembly, the single SQL query condition is returned. If the node is a branch node, first declare an empty SQL query condition statement `resultSQL`. Then, recursively parse all subtrees of that branch node. After each recursive iteration, a temporary variable is used to receive the return value. The temporary variable storing the subtree query conditions is enclosed in parentheses and concatenated with the `resultSQL` statement, using the logical operators of that branch node. After all subtrees have been recursively traversed, `resultSQL` stores the SQL query conditions formed by the subtree rooted at that branch node. Finally, `resultSQL` is returned. After the complete recursion, null values in the query conditions are removed. The specific operations for defining a set of operators include: Users first need to customize a set of operators according to their own business needs, which will be used to construct SQL query conditions in the subsequent condition generation process; The operator set is a collection of logical operators, comparison operators, and concatenation operators, along with their corresponding query types. The specific steps to obtain tree-structured JSON data include: the front-end concatenates the tree-structured JSON data and sends it to the back-end.
2. The method for converting tree-structured JSON data into SQL query conditions according to claim 1, characterized in that: The specific steps for defining database mapping rules include: Define database mapping rules to map attributes in the tree-structured JSON data to columns in the database table for use in subsequent condition generation and SQL statement construction.
3. The method for converting tree-structured JSON data into SQL query conditions according to claim 1, characterized in that: The specific steps for verifying relevant data include: Perform relevant validations on the tree-structured JSON data passed from the front end, including checking the validity of the JSON data, the correctness of the structure, and the accuracy of the data type.
4. The method for converting tree-structured JSON data into SQL query conditions according to claim 1, characterized in that: The specific operations for assembling SQL statements and executing database queries include: Based on the parsed conditions and other parameters passed from the front end, the SQL statement is assembled, including constructing a SELECT statement, adding a FROM clause, applying connection operations, executing database query operations, and sending the SQL statement to the database system to retrieve data that meets the conditions.
Citation Information
Patent Citations
Method and system for realizing SQL query condition based on tree structure
CN106919606A
Method and system based on JSON (JavaScript Object Notation) statement operation database
CN106980617A