CN-122019652-A - JSON data conversion method and device
Abstract
The application discloses a JSON data conversion method and a JSON data conversion device. The method comprises the steps of receiving a conversion configuration file, including a field mapping rule list, wherein each field mapping rule defines a source path, a conversion expression and a target path, analyzing the source path and the target path, compiling the conversion expression to generate script examples, receiving source JSON data to be converted, determining a plurality of field mapping rules corresponding to a plurality of fields, determining whether a final result template is defined in the conversion configuration file, entering a template driving conversion mode if the final result template is present, entering a direct mapping conversion mode if the final result template is not present, generating target JSON data according to the template mapping rules and the field mapping rules in the template driving conversion mode, and generating target JSON data according to the field mapping rules in the direct mapping conversion mode. The application can define complex structure conversion and logic processing through simple JSON conversion configuration file, and realize low-code and high-efficiency data integration.
Inventors
- HOU YIQING
- TAN XIAOWEI
Assignees
- 数数信息科技(上海)有限公司
Dates
- Publication Date
- 20260512
- Application Date
- 20260204
Claims (12)
- 1. A JSON data conversion method for an electronic device, comprising: A configuration file receiving step, which is to receive a conversion configuration file, wherein the conversion configuration file comprises a field mapping rule list, and each field mapping rule in the field mapping rule list defines a source path, a conversion expression and a target path; A precompiled step, wherein the source path and the target path in each field mapping rule are analyzed to obtain an analyzed source path and an analyzed target path, and the conversion expression is compiled to generate a reusable script instance; A data receiving step, namely receiving source JSON data to be converted, wherein the source JSON data comprises a plurality of fields, and determining a plurality of field mapping rules which are respectively in one-to-one correspondence with the plurality of fields from the field mapping rule list; A mode determining step, determining a conversion mode according to whether a final result template is defined in the conversion configuration file, wherein if the final result template exists, the mode enters a template driving conversion mode, and if the final result template does not exist, the mode enters a direct mapping conversion mode; A conversion step of generating target JSON data from the source JSON data in accordance with a template mapping rule and the plurality of field mapping rules in the final result template in the template-driven conversion mode, And generating the target JSON data from the source JSON data according to the field mapping rules in the direct mapping conversion mode.
- 2. The JSON data conversion method of claim 1, in which in the template driven conversion mode, a plurality of fixed fields in the final result template are filled in according to the template mapping rule, and the plurality of fields of the source JSON data are converted according to the plurality of field mapping rules to generate the target JSON object, wherein the filled plurality of fixed fields and the target JSON object form the target JSON data, And under the direct mapping conversion mode, converting the fields of the source JSON data according to the field mapping rules to generate the target JSON object as the target JSON data.
- 3. The JSON data conversion method of claim 2, wherein converting the plurality of fields of the source JSON data, respectively, in accordance with the plurality of field mapping rules includes: Traversing the field mapping rules, extracting a data value from the source JSON data by utilizing the parsed source path aiming at each field mapping rule, executing logic operation defined by the conversion expression on the data value by utilizing the precompiled script instance, and writing an operation result into a position appointed by the parsed target path of the target JSON object.
- 4. The JSON data conversion method of claim 3, wherein each field mapping rule further includes a target data type, Before writing the operation result, converting the operation result into a corresponding data type according to the target data type in the field mapping rule.
- 5. The JSON data conversion method of claim 1, wherein each field in the source JSON data includes a source path, and a plurality of field mapping rules corresponding one-to-one to each of the plurality of fields are determined from the list of field mapping rules based on the source path of each field.
- 6. The JSON data conversion method of claim 1, in which compiling the conversion expression in the pre-compiling step includes: detecting whether JSONPath references are contained in the conversion expression; If so, extracting JSONPath references and replacing the references with standard variable placeholders so as to generate a standard script character string; Compiling the standard script character string to generate the script instance.
- 7. The method of claim 1, wherein in the template driven conversion mode, the template mapping rules are used to generate values for a plurality of fixed fields independent of the source JSON data, the values for the plurality of fixed fields being dynamically invoked values or default values.
- 8. A method according to claim 3, characterized in that the method further comprises: When the data value extracted from the source path is an array, the field mapping rule is independently applied to each element object in the array, converted sub-objects are generated, all the converted sub-objects are aggregated into a list, and the list is assigned to the position appointed by the analyzed target path.
- 9. A JSON data conversion device, comprising: A configuration file receiving unit for receiving a conversion configuration file, wherein the conversion configuration file comprises a field mapping rule list, and each field mapping rule in the field mapping rule list defines a source path, a conversion expression and a target path; The precompiled unit analyzes the source path and the target path in each field mapping rule to obtain an analyzed source path and an analyzed target path, compiles the conversion expression and generates a reusable script instance; The data receiving unit is used for receiving source JSON data to be converted, wherein the source JSON data comprises a plurality of fields, and a plurality of field mapping rules which are respectively in one-to-one correspondence with the plurality of fields are determined from the field mapping rule list; The mode determining unit determines a conversion mode according to whether a final result template is defined in the conversion configuration file, wherein if the final result template exists, the mode enters a template driving conversion mode, and if the final result template does not exist, the mode enters a direct mapping conversion mode; a conversion unit which generates target JSON data from the source JSON data in accordance with a template mapping rule and the plurality of field mapping rules in the final result template in the template-driven conversion mode, And generating the target JSON data from the source JSON data according to the field mapping rules in the direct mapping conversion mode.
- 10. A computer readable storage medium having instructions stored thereon which, when executed on a computer, cause the computer to perform the JSON data conversion method of any one of claims 1 to 8.
- 11. An electronic device comprising one or more processors, one or more memories, the one or more memories storing one or more programs that, when executed by the one or more processors, cause the electronic device to perform the JSON data conversion method of any of claims 1-8.
- 12. A computer program product comprising computer executable instructions that are executed by a processor to implement the JSON data conversion method of any one of claims 1-8.
Description
JSON data conversion method and device Technical Field The application relates to the technical field of data processing, in particular to a JSON data conversion method, a device, a medium, electronic equipment and a computer program product. Background Along with the rapid development of micro-service architecture, mobile internet and internet of things, JSON (JavaScript Object Notation) has become a de facto standard format for data exchange between heterogeneous systems. In the actual software development and system integration process, because of the differences of data structure definition of different service systems (such as front-end application, back-end service, third-party API, legacy system), the transmitted JSON data often needs to be cleaned, reorganized, mapped and format converted. Conventional conversion methods generally adopt hard coding, an object mapping framework or a special JSON conversion library (such as Joit, etc.), however, the methods have the problems of poor flexibility, low performance, difficult maintenance, etc. Disclosure of Invention Aiming at the defects, the invention provides a JSON data conversion method based on a configurational and expression engine, which can define complex structural conversion and logic processing through a simple JSON conversion configuration file and solves the performance problem by utilizing a pre-compiling technology, thereby realizing low-code and high-efficiency data integration. The embodiment of the application provides a JSON data conversion method, a JSON data conversion device, a medium, electronic equipment and a computer program product. In a first aspect, an embodiment of the present application provides a JSON data conversion method, which is used in an electronic device, and includes: A configuration file receiving step, which is to receive a conversion configuration file, wherein the conversion configuration file comprises a field mapping rule list, and each field mapping rule in the field mapping rule list defines a source path, a conversion expression and a target path; A precompiled step, wherein the source path and the target path in each field mapping rule are analyzed to obtain an analyzed source path and an analyzed target path, and the conversion expression is compiled to generate a reusable script instance; A data receiving step, namely receiving source JSON data to be converted, wherein the source JSON data comprises a plurality of fields, and determining a plurality of field mapping rules which are respectively in one-to-one correspondence with the plurality of fields from the field mapping rule list; A mode determining step, determining a conversion mode according to whether a final result template is defined in the conversion configuration file, wherein if the final result template exists, the mode enters a template driving conversion mode, and if the final result template does not exist, the mode enters a direct mapping conversion mode; A conversion step of generating target JSON data from the source JSON data in accordance with a template mapping rule and the plurality of field mapping rules in the final result template in the template-driven conversion mode, And generating the target JSON data from the source JSON data according to the field mapping rules in the direct mapping conversion mode. In a second aspect, an embodiment of the present application provides a JSON data conversion device, including: A configuration file receiving unit for receiving a conversion configuration file, wherein the conversion configuration file comprises a field mapping rule list, and each field mapping rule in the field mapping rule list defines a source path, a conversion expression and a target path; The precompiled unit analyzes the source path and the target path in each field mapping rule to obtain an analyzed source path and an analyzed target path, compiles the conversion expression and generates a reusable script instance; The data receiving unit is used for receiving source JSON data to be converted, wherein the source JSON data comprises a plurality of fields, and a plurality of field mapping rules which are respectively in one-to-one correspondence with the plurality of fields are determined from the field mapping rule list; The mode determining unit determines a conversion mode according to whether a final result template is defined in the conversion configuration file, wherein if the final result template exists, the mode enters a template driving conversion mode, and if the final result template does not exist, the mode enters a direct mapping conversion mode; a conversion unit which generates target JSON data from the source JSON data in accordance with a template mapping rule and the plurality of field mapping rules in the final result template in the template-driven conversion mode, And generating the target JSON data from the source JSON data according to the field mapping rules in the direct mapping conversion