A data comparison method and device

By determining the unique key names in the unordered JSON array and comparing, the problem of poor accuracy of the unordered JSON array in the prior art is solved, and a more accurate JSON data comparison result is achieved.

CN112711419BActive Publication Date: 2025-02-14MASHANG CONSUMER FINANCE CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN201911017739.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2019-10-24
Publication Date
2025-02-14
Estimated Expiration
2039-10-24

AI Technical Summary

Technical Problem

In the prior art, the comparison results of disordered JSON arrays have poor accuracy.

Method used

When the JSON data to be compared is of JSON array type, it determines its unique key name, and determines the target objects corresponding to each object based on these key names, and then compares them separately.

Benefits of technology

Improves the accuracy of the comparison results of unordered JSON arrays, ensuring accurate comparison of JSON data of two JSON array types.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN112711419B_ABST
    Figure CN112711419B_ABST
Patent Text Reader

Abstract

The present invention provides a data comparison method and device, the method comprising: when the first JSON data to be compared is of JSON array type, determining the unique key name of the first JSON data; each object in the first JSON data includes the unique key name, and the key values ​​corresponding to the unique key names of different objects in the first JSON data are different; determining the target object corresponding to each object in the first JSON data according to the unique key name; the target object is an object in the second JSON data to be compared; and comparing each object in the first JSON data with its corresponding target object respectively. Through the data comparison method provided by the present invention, the comparison objects corresponding to the JSON data of two JSON array types can be determined more accurately, thereby improving the accuracy of the comparison results of unordered JSON arrays.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of information processing technology, and in particular to a data comparison method and device. Background Art

[0002] JSON (JavaScript Object Notation) is a lightweight data exchange format that is widely used in daily software development. Both developers and testers need to frequently compare and verify data in this format. There are currently some comparison tools, such as Beyond Compare, Diff, etc., which can do simple comparison work. However, for some more complex JSON data, such as unordered JSON arrays (i.e., JSONArray), there is no more suitable comparison logic in the prior art, resulting in poor accuracy of the comparison results of unordered JSON arrays. Summary of the invention

[0003] The embodiment of the present invention provides a data comparison method and device to solve the problem of poor accuracy of comparison results of unordered JSON arrays in the prior art.

[0004] In order to solve the above-mentioned technical problems, the present invention is achieved as follows:

[0005] In a first aspect, an embodiment of the present invention provides a data comparison method. The method comprises:

[0006] In the case where the first JSON data to be compared is of JSON array type, determining a unique key name of the first JSON data; each object in the first JSON data includes the unique key name, and key values ​​corresponding to the unique key names of different objects in the first JSON data are different;

[0007] Determine the target object corresponding to each object in the first JSON data according to the unique key name; the target object is the object in the second JSON data to be compared;

[0008] Each object in the first JSON data is compared with its corresponding target object.

[0009] In a second aspect, an embodiment of the present invention further provides a data comparison device. The data comparison device comprises:

[0010] A first determination module is used to determine a unique key name of the first JSON data when the first JSON data to be compared is of a JSON array type; each object in the first JSON data includes the unique key name, and key values ​​corresponding to the unique key names of different objects in the first JSON data are different;

[0011] A second determination module is used to determine the target object corresponding to each object in the first JSON data according to the unique key name; the target object is the object in the second JSON data to be compared;

[0012] The first comparison module is used to compare each object in the first JSON data with its corresponding target object.

[0013] In a third aspect, an embodiment of the present invention further provides a data comparison device, comprising a processor, a memory, and a computer program stored in the memory and executable on the processor, wherein the computer program implements the steps of the above-mentioned data comparison method when executed by the processor.

[0014] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the steps of the above-mentioned data comparison method are implemented.

[0015] In an embodiment of the present invention, when the first JSON data to be compared is of JSON array type, the unique key name of the first JSON data is determined; each object in the first JSON data includes the unique key name, and the key values ​​corresponding to the unique key names of different objects in the first JSON data are different; the target object corresponding to each object in the first JSON data is determined according to the unique key name; the target object is an object in the second JSON data to be compared; and each object in the first JSON data is compared with its corresponding target object. Since the comparison objects corresponding to the two JSON array type JSON data can be determined more accurately based on the unique key name, the accuracy of the comparison result of the unordered JSON array can be improved. BRIEF DESCRIPTION OF THE DRAWINGS

[0016] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings required for use in the description of the embodiments of the present invention will be briefly introduced below. Obviously, the accompanying drawings in the following description are only some embodiments of the present invention. For ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without paying creative labor.

[0017] Figure 1 is a flow chart of a data comparison method provided by an embodiment of the present invention;

[0018] Figure 2 is a flow chart of a data comparison method provided by another embodiment of the present invention;

[0019] Figure 3is a flow chart of a data comparison method provided by another embodiment of the present invention;

[0020] Figure 4 is a structural diagram of a data comparison device provided by an embodiment of the present invention;

[0021] Figure 5 It is a structural diagram of a data comparison device provided by another embodiment of the present invention. DETAILED DESCRIPTION

[0022] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0023] The embodiment of the present invention provides a data comparison method. Figure 1 , Figure 1 is a flow chart of a data comparison method provided by an embodiment of the present invention, such as Figure 1 As shown, the following steps are included:

[0024] Step 101: When the first JSON data to be compared is of a JSON array type, determine a unique key name of the first JSON data; each object in the first JSON data includes the unique key name, and key values ​​corresponding to the unique key names of different objects in the first JSON data are different.

[0025] In this embodiment, since each object in the first JSON data includes the unique key name, and the key values ​​corresponding to the unique key names of different objects in the first JSON data are different, different objects in the first JSON data can be identified by the unique key name.

[0026] For example, the first JSON data is as follows: [{"key1":"v11","key2":"v21","key3":"v31"},{"key1":"v12","key2":"v21","key3":"v32"},{"key1":"v13","key2":"v21","key3":"v32"}], since the key values ​​of key1 of different objects in the first JSON data are different, different objects in the first JSON data can be identified by key1, so that the unique key name of the first JSON data can be key1.

[0027] Step 102: Determine the target object corresponding to each object in the first JSON data according to the unique key name; the target object is the object in the second JSON data to be compared.

[0028] In this embodiment, the key value of the unique key name in each object in the first JSON data and the key value of the unique key name in its corresponding target object can be the same, that is, two objects with the same key value of the unique key name in the second JSON data and the first JSON data can be compared.

[0029] For example, the first JSON data is as follows: [{"key1":"v11","key2":"v21","key3":"v31"},{"key1":"v12","key2":"v21","key3":"v32"},{"key1":"v13","key2":"v21","key3":"v32"}]; the second JSON data is as follows: [{"key1":"v12","key2":"v21","key3":"v31"},{"key1":"v13","key2":"v21","key3":"v31"},{"key1":"v11","key2":"v22","key3":"v31"}]; then the first JSON data and In the second JSON data, two objects with the same key value of key1 can be compared, that is, {"key1":"v11","key2":"v21","key3":"v31"} can be compared with {"key1":"v11","key2":"v22","key3":"v31"}, {"key1":"v12","key2":"v21","key3":"v32"} can be compared with {"key1":"v12","key2":"v21","key3":"v31"}, and {"key1":"v13","key2":"v21","key3":"v32"} can be compared with {"key1":"v13","key2":"v21","key3":"v31"}.

[0030] It should be noted that, each object in the above-mentioned first JSON data may be each array element in the first JSON data, and the target object corresponding to each object in the above-mentioned first JSON data may be each array element in the second JSON data; each object in the above-mentioned first JSON data may also be each JSON object converted from the first JSON data, and the target object corresponding to each object in the above-mentioned first JSON data may also be each JSON object converted from the second JSON data, and this embodiment does not limit this.

[0031] In this step, the two objects to be compared in the first JSON data and the second JSON data can be more accurately determined through the unique key name.

[0032] Step 103: Compare each object in the first JSON data with its corresponding target object.

[0033] For example, the key names of each object in the first JSON data can be traversed separately, and it can be determined whether the same key name exists in the corresponding target object. If so, the key value corresponding to the key name in the object is compared with the key value corresponding to the key name in the corresponding target object.

[0034] It should be noted that if each object in the above-mentioned first JSON data is a JSON object, and the target object corresponding to each object in the above-mentioned first JSON data is also a JSON object, the comparison can be directly based on the granularity of the JSON object, or the JSON object can be further converted into a single-value type object and compared based on the converted single-value type object. This embodiment does not limit this.

[0035] Optionally, if there are still key values ​​of the JSON array type in each object in the first JSON data, the key values ​​of the JSON array type can be compared in the same manner as steps 101 to 103 above, which will not be repeated here.

[0036] It should be noted that the above-mentioned first JSON data can be the input JSON data to be compared, or it can be a key value in the input JSON data to be compared. Similarly, the above-mentioned second JSON data can be the input JSON data to be compared, or it can be a key value in the input JSON data to be compared. This embodiment does not limit this.

[0037] In an embodiment of the present invention, when the first JSON data to be compared is of JSON array type, the unique key name of the first JSON data is determined; each object in the first JSON data includes the unique key name, and the key values ​​corresponding to the unique key names of different objects in the first JSON data are different; the target object corresponding to each object in the first JSON data is determined according to the unique key name; the target object is an object in the second JSON data to be compared; and each object in the first JSON data is compared with its corresponding target object. Since the comparison objects corresponding to the two JSON array type JSON data can be determined more accurately based on the unique key name, the accuracy of the comparison result of the unordered JSON array can be improved.

[0038] Optionally, the above step 102, i.e., determining the unique key name corresponding to the first JSON data, may include:

[0039] Determine a unique key name of the first JSON data according to the key name and its corresponding key value in each object in the first JSON data; or

[0040] Receive a unique key name of the first JSON data specified by a user.

[0041] In one embodiment, the key name and its corresponding key value in each object in the first JSON data can be obtained respectively, and a key name included in each object and with different corresponding key values ​​in each object is selected as a unique key name, which can improve the flexibility of determining the unique key name corresponding to the JSON array.

[0042] In another embodiment, the user may specify a unique key name corresponding to the JSON array. For example, the user may pre-configure the unique key names corresponding to each JSON array included in the first JSON object before data comparison, which may improve the efficiency of data comparison.

[0043] Optionally, the first JSON data is a first key value in a first JSON object to be compared, the second JSON data is a second key value in a second JSON object to be compared, and the key name corresponding to the first key value is the same as the key name corresponding to the second key value.

[0044] In this embodiment, the first key value and the second key value may be key values ​​at the same level and corresponding to the same key name in the first JSON object and the second JSON object, respectively. In actual situations, a JSON object may include at least one key-value pair, i.e., a key name (i.e., Key) and a key value (i.e., Value), and the key value in each key-value pair may be a JSON array (i.e., JSONArray), a JSON object (i.e., JSONObject), and so on, so that a JSON object may have multiple levels, each level may include at least one node, and in the data comparison process, the key values ​​at the same level and corresponding to the same key name in the comparison base object and the compared object are usually compared.

[0045] Optionally, the first JSON object may be a JSON object converted from a first JSON array to be compared (i.e., JSONArray), and the second JSON object may be a JSON object converted from a second JSON array to be compared, wherein the second JSON object may be a compared object of the first JSON object determined based on the unique key name corresponding to the first JSON array.

[0046] For example, when the input comparison base object and the compared object are both JSON arrays, the JSON array of the comparison base object and the JSON array of the compared object can be converted into JSON objects, and the two JSON objects to be compared in the comparison base object and the compared object can be determined based on the unique key name corresponding to the JSON array of the comparison base object.

[0047] Optionally, the first JSON object is configured as a comparison reference object, and the second JSON object is configured as a compared object;

[0048] In the case where the first JSON data to be compared is of a JSON array type, before determining the unique key name of the first JSON data, the method further includes:

[0049] Traverse each key name of the first JSON object, and determine whether there is a key name in the second JSON object that is the same as the key name of the first JSON object;

[0050] If so, obtain the first key value in the first JSON object and the second key value in the second JSON object respectively.

[0051] In this embodiment, the user can configure the first JSON object as the comparison reference object and the second JSON object as the object to be compared. Compared with the prior art in which the object with more elements in the two JSON objects is used as the comparison reference object and the object with fewer elements is used as the object to be compared, it is convenient for the user to know the position of the expected value and the actual value more clearly. In addition, since the comparison reference object can be configured by the user, the user can set the elements or fields included in the comparison reference object according to actual needs, so that some fields that are not to be compared can be filtered out more conveniently. Compared with the prior art in which the filtering fields are actively configured, the convenience of the field filtering operation and the efficiency of data comparison can be improved.

[0052] For example, you can pre-specify the first JSON object as the comparison reference object and the second JSON object as the compared object, and then you can traverse the key names of the first JSON object and determine whether there are key names in the second JSON object that are the same as the key names of the first JSON object. If so, you can respectively obtain the key values ​​corresponding to the same key names in the first JSON object and the second JSON object and compare them.

[0053] Optionally, before the above step 101, that is, when the first JSON data to be compared is of a JSON array type, before determining the unique key name of the first JSON data, the method further includes:

[0054] Get the first JSON string and the second JSON string;

[0055] The first JSON string and the second JSON string are respectively converted into JSON object types to obtain the first JSON object and the second JSON object.

[0056] In this embodiment, when the object to be compared is a JSON string, the JSON string may be converted into a JSON object type before comparison.

[0057] Optionally, during the process of converting the first JSON string and the second JSON string to a JSON object type, exception capture can be performed (for example, exception capture can be performed through try / catch) to determine whether the first JSON string and the second JSON string are successfully converted to JSON objects, that is, to determine whether the first JSON string and the second JSON string are standard JSON strings. If the conversion of the first JSON string and the second JSON string is successful, the first JSON object and the second JSON object can be obtained. If the conversion of the first JSON string and / or the second JSON string fails, the process can be terminated.

[0058] In actual applications, many business interfaces often generate data of JSON string type. By converting JSON strings into JSON object types before comparison, the convenience of JSON string comparison can be improved.

[0059] Optionally, the method may further include:

[0060] When the data type of the first JSON data is a single-value type, comparing the first JSON data with the second JSON data;

[0061] If the first JSON data is different from the second JSON data, a target key name, the first JSON data, and the second JSON data are stored, where the target key name is a key name corresponding to the first JSON data or a key name corresponding to the second JSON data.

[0062] In this embodiment, when the data type of the first JSON data (that is, the first key value) is a single-value type, the first JSON data can be directly compared with the second JSON data (that is, the second key value). If the two are different, the target key name, the first JSON data and the second JSON data can be recorded to facilitate problem location.

[0063] Optionally, the method may further include:

[0064] When the data type of the first JSON data is a JSON object type, traverse each key name in the first JSON data, and determine whether there is a key name in the second JSON data that is the same as the key name in the first JSON data;

[0065] If the first key name in the first JSON data does not exist in the second JSON data, storing the first key name and the key value corresponding to the first key name;

[0066] If the second key name in the first JSON data exists in the second JSON data, obtain the third key value corresponding to the second key name in the first JSON data and the fourth key value corresponding to the second key name in the second JSON data respectively;

[0067] If the third key value is different from the fourth key value, the second key name, the third key value and the fourth key value are stored.

[0068] In this embodiment, the first key name and the second key name can be any key name in the first JSON data (for example, the first key value). Specifically, for a key name in the first JSON data that does not exist in the second JSON data (for example, the second key value), the key name and the key value corresponding to the key name can be stored; for a key name in the first JSON data that exists in the second JSON data, the key value corresponding to the key name in the first JSON data and the second JSON data can be obtained and compared respectively. If the two are different, the key name and the key value corresponding to the key name in the first JSON data and the second JSON data can be stored. For example, they can be stored in the order of key name, key value in the benchmark comparison object, and key value of the compared object; if the two are the same, they can be not stored.

[0069] This embodiment traverses each key name in the first JSON data for comparison. Compared with the prior art, which exits the comparison as soon as one mismatch is determined (for example, length mismatch), it can achieve a full comparison of JSON data and enrich the comparison results of JSON data. In addition, different key names and key values ​​in the first JSON data and the second JSON data are recorded to facilitate later problem location.

[0070] Optionally, when the data type of the first JSON data is a JSON object type, the first JSON data may be converted into a single-value type before comparison, and different contents in the comparison result may be recorded.

[0071] The following is an explanation of the embodiments of the present invention with reference to examples:

[0072] See also Figure 2 The data comparison method provided by the embodiment of the present invention may include the following steps:

[0073] Step 201: Get a first JSON string.

[0074] The first JSON string may be a comparison reference object.

[0075] Step 202: Convert the first JSON string into a JSONObject object.

[0076] Step 203: Whether the conversion is successful.

[0077] In this step, if the first JSON string is successfully converted into a JSONObject object, the first JSONObject can be obtained; otherwise, the process can be exited and the result can be returned.

[0078] Step 204: Iterate and obtain the key name of the first JSONObject.

[0079] In this step, each key name in the first JSONObject may be traversed.

[0080] Step 205: Determine whether the second JSONObject contains the key name.

[0081] In this step, the second JSONObject is the object to be compared. For each key name in the first JSONObject, it can be determined whether the second JSONObject includes the key name. If it does, step 206 is executed.

[0082] Step 206: Get the key value.

[0083] In this step, the key values ​​corresponding to the same key name are respectively obtained from the first JSONObject and the second JSONObject, and the flow processing can be performed according to the type of the key value corresponding to the key name in the first JSONObject.

[0084] Step 207: If it is of JSONArray type, convert it to JSONObject type for comparison.

[0085] In this step, if the key value corresponding to the key name in the first JSONObject is of JSONArray type, the unique key name corresponding to the key value can be determined, and the key value can be converted into JSONObject type and then compared based on the unique key name of the key value, that is, the JSONObject objects in the compared object corresponding to each JSONObject object in the comparison base object are determined based on the determined unique key name for comparison.

[0086] Step 208: If it is of JSONObject type, convert it to a single value type for comparison.

[0087] In this step, if the key value corresponding to the key name in the first JSONObject is of JSONObject type, the key value corresponding to the key name in the first JSONObject may be converted into a single-value type for comparison.

[0088] It should be noted that if the upper level of the key value is of JSONArray type, the corresponding JSONObject to be compared can be determined according to the unique key name corresponding to the upper level.

[0089] Step 209: If it is a single-value type, directly compare.

[0090] In this step, if the key value corresponding to the key name in the first JSONObject is a single-value type, the key value corresponding to the key name in the first JSONObject can be directly compared with the key value corresponding to the key name in the second JSONObject.

[0091] Step 210: cache the key names and key values ​​that are different after comparison.

[0092] For the comparison results obtained based on the above steps 207 to 209, different key names and key values ​​in the comparison results can be cached. For example, for the comparison results in which the key values ​​of the comparison base object and the compared object are different, the key name, the key value of the comparison base object and the key value of the compared object can be cached; for the comparison results in which the compared object does not include the key name of the comparison base object, the key name and key value of the comparison base object can be cached.

[0093] Step 211: Get a second JSON string.

[0094] The second JSON string may be a compared object.

[0095] Step 212: Convert the second JSON string into a JSONObject object.

[0096] Step 213: Whether the conversion is successful.

[0097] In this step, if the second JSON string is successfully converted into a JSONObject object, the second JSONObject can be obtained; otherwise, the process can be exited and the result can be returned.

[0098] See also Figure 3 The data comparison method provided by the embodiment of the present invention may include the following steps:

[0099] Step 301: Get a first JSON string.

[0100] The first JSON string may be a comparison reference object.

[0101] Step 302: Convert the first JSON string into JSON data of the target type.

[0102] The target type may include a JSON object type or a JSON array type, etc.

[0103] Step 303: Whether the conversion is successful.

[0104] In this step, if the first JSON string is successfully converted into JSON data of the target type, the first target JSON data of the target type, for example, the first JSONObject, can be obtained; otherwise, the process can be exited and the result can be returned.

[0105] Similarly, this embodiment can convert the second JSON string into JSON data of the target type, and if the conversion of the second JSON string into JSON data of the target type is successful, the first target JSON data of the target type, for example, the second JSONObject, can be obtained, otherwise the process can be exited and the result can be returned. The second JSON string is the object to be compared.

[0106] It should be noted that, in this embodiment, the first JSON string may be designated as a comparison reference object, the second JSON string may be designated as a comparison object, and the number of elements included in the first JSON string may be less than the number of elements included in the second JSON string.

[0107] Step 304: Is it a JSONObject?

[0108] In this step, it can be determined whether the input data is a JSONObject, if so, step 305 is executed, otherwise step 310 is executed. For example, it is determined whether the first target JSON data obtained by converting the first JSON string or the key value in the obtained first target JSON data is a JSONObject, if so, step 305 is executed, otherwise step 310 is executed.

[0109] It should be noted that for JSONObject, you can first convert the JSONArray into a single-value type object and then perform the comparison.

[0110] Step 305: Check whether there are any untaken key names at the current level.

[0111] In this step, it can be determined whether the current level of the input data (such as the level currently being traversed) has any untaken key names. If so, step 306 is executed, otherwise step 307 can be executed.

[0112] For example, if the first target JSON data is JSONObject, it can be determined whether there are any untaken (such as untraversed) key names at the current level of the first target JSON data; if the key value in the acquired first target JSON data is JSONObject, it can be determined whether there are any untaken (such as untraversed) key names at the current level of the key value.

[0113] In actual applications, the key names of each level of the comparison reference object can be traversed, and it can be determined whether the compared reference object includes the corresponding key name at the same level.

[0114] Step 306: Get the key name.

[0115] In this step, you can take the key name that has not been taken at the current level.

[0116] Step 307: Check whether there is any untaken key name in the previous level.

[0117] In this step, it can be determined whether there is a key name that has not been taken (such as not traversed) in the previous level of the current level. If so, step 306 is executed. Otherwise, a cached result with the comparison base object as a reference can be returned. The cached result can include different key names and key values ​​in the comparison result. For example, for the comparison result in which the key values ​​of the comparison base object and the compared object are different, the cached result can include the key name, the key value of the comparison base object and the key value of the compared object; for the comparison result in which the compared object does not include the key name of the comparison base object, the cached result can include the key name and key value of the comparison base object.

[0118] Step 308: Determine whether the second target JSON data at the same level contains the key name.

[0119] In this step, the second target JSON data is the object to be compared. If the second target JSON data contains the key name at the same level, step 309 is executed, otherwise the comparison result (key name + key value) is added to the cache.

[0120] Step 309: Get the key value.

[0121] In this step, the key values ​​corresponding to the key name taken in step 307 in the first target JSON data and the second target JSON data can be respectively obtained, and the type of the key value corresponding to the key name taken in step 307 in the first target JSON data can be returned.

[0122] Step 310: Is it JSONArray?

[0123] In this step, it can be determined whether the input data is a JSONArray, if so, step 311 is executed, otherwise step 315 is executed. For example, it is determined whether the first target JSON data obtained by converting the first JSON string or the key value in the obtained first target JSON data is a JSONArray, if so, step 311 is executed, otherwise step 315 is executed.

[0124] It should be noted that for JSONArray, you can first convert the JSONArray to JSONObject and then perform the comparison.

[0125] Step 311: Confirm the unique key name.

[0126] In this step, if the input data is a JSONArray, the unique key name of the input data can be determined first as a basis for obtaining the compared object before comparison.

[0127] Specifically, the key names of all the leaf nodes in the array elements (such as JSONObject) in the JSONArray whose key values ​​are all different can be automatically obtained and used as the unique key name; or the unique key name can be specified by the user.

[0128] Step 312: Iterate and obtain the key names in the array elements.

[0129] In this step, the key name of each data element in the JSONArray of the comparison benchmark object can be iterated.

[0130] Step 313: Based on the unique key name, determine whether the second target JSON data at the same level contains the key name.

[0131] In this step, the array elements in the second target JSON data at the same level corresponding to each data element in the JSONArray of the comparison reference object can be determined based on the unique key name determined in the above step 311, and it is determined whether the array elements in the second target JSON data at the same level include the key name taken in the above step 312. Specifically, if the array elements in the second target JSON data at the same level include the key name, step 314 is executed, otherwise the comparison result (such as: key name + key value) is added to the cache.

[0132] Step 314: Get the key value.

[0133] In this step, the key values ​​corresponding to the key name obtained in step 312 in the two data elements to be compared can be obtained respectively, and the type of the key value corresponding to the key name obtained in step 312 in the array element of the comparison reference object can be returned.

[0134] Step 315: Whether it is another type of object.

[0135] In this step, the above-mentioned other type objects may be single-value type objects.

[0136] Step 316: Are they equal?

[0137] In this step, it can be determined whether the single-value type object of the comparison reference object and the single-value type object in the corresponding compared object are equal. For example, if the key value of the comparison reference object obtained in step 309 is a single-value type object, the key value of the comparison reference object obtained in step 309 and the key value of the compared object can be compared. If they are not equal, the comparison result (such as: key name + key value of the comparison reference object + key value of the compared object) can be added to the cache.

[0138] The data comparison method provided by the embodiment of the present invention can partially compare unordered JSON strings, which is convenient for users to mine unequal items of the comparison objects for application in development, debugging, and test verification. Locating the items to be compared according to the reference object can facilitate users to clearly know the positions of the expected value and the actual value; and a comparison reference object whose number of elements is greater than, equal to, or less than the number of elements of the object to be compared can be specified, which is convenient for users to flexibly manage key names that do not need to be compared or cannot be located. For the comparison of unordered JSONArray, the compared objects can be accurately obtained, which can improve the accuracy of the comparison results compared to the prior art that relies on self-sequential iteration for comparison. In addition, the key names and key values ​​of the different parts can be recorded in the dimension of rows, which is conducive to rapid problem location.

[0139] See also Figure 4 , Figure 4 is a structural diagram of a data comparison device provided by an embodiment of the present invention. Figure 4 As shown, the data comparison device 400 includes:

[0140] A first determination module 401 is used to determine a unique key name of the first JSON data when the first JSON data to be compared is of a JSON array type; each object in the first JSON data includes the unique key name, and key values ​​corresponding to the unique key names of different objects in the first JSON data are different;

[0141] A second determination module 402 is used to determine the target object corresponding to each object in the first JSON data according to the unique key name; the target object is the object in the second JSON data to be compared;

[0142] The first comparison module 403 is used to compare each object in the first JSON data with its corresponding target object.

[0143] Optionally, the first determining module is specifically configured to:

[0144] Determine a unique key name of the first JSON data according to the key name and its corresponding key value in each object in the first JSON data; or

[0145] Receive a unique key name of the first JSON data specified by a user.

[0146] Optionally, the first JSON data is a first key value in a first JSON object to be compared, the second JSON data is a second key value in a second JSON object to be compared, and the key name corresponding to the first key value is the same as the key name corresponding to the second key value.

[0147] Optionally, the first JSON object is configured as a comparison reference object, and the second JSON object is configured as a compared object;

[0148] The device also includes:

[0149] A first traversal module is used for, when the first JSON data to be compared is of a JSON array type, traversing each key name of the first JSON object before determining a unique key name of the first JSON data, and determining whether there is a key name in the second JSON object that is the same as a key name of the first JSON object;

[0150] The first acquisition module is used to respectively acquire the first key value in the first JSON object and the second key value in the second JSON object if they exist.

[0151] Optionally, the device further comprises:

[0152] A second acquisition module is used to acquire the first JSON string and the second JSON string before determining the unique key name of the first JSON data when the first JSON data to be compared is of a JSON array type;

[0153] The conversion module is used to convert the first JSON string and the second JSON string into JSON object types respectively to obtain the first JSON object and the second JSON object.

[0154] Optionally, the device further comprises:

[0155] A second comparison module, configured to compare the first key value with the second key value when the data type of the first key value is a single-value type;

[0156] The first storage module is used to store a target key name, the first key value and the second key value if the first key value is different from the second key value, wherein the target key name is a key name corresponding to the first key value or a key name corresponding to the second key value.

[0157] Optionally, the device further comprises:

[0158] A second traversal module is used for traversing each key name in the first JSON data when the data type of the first JSON data is a JSON object type, and determining whether there is a key name in the second JSON data that is the same as a key name in the first JSON data;

[0159] A second storage module, configured to store the first key name and a key value corresponding to the first key name if the first key name in the first JSON data does not exist in the second JSON data;

[0160] A third acquisition module, configured to respectively acquire a third key value corresponding to the second key name in the first JSON data and a fourth key value corresponding to the second key name in the second JSON data if the second key name in the first JSON data exists in the second JSON data;

[0161] The third storage module is used to store the second key name, the third key value and the fourth key value if the third key value is different from the fourth key value.

[0162] The data comparison device 400 provided in the embodiment of the present invention can implement each process in the above method embodiment, and will not be described again here to avoid repetition.

[0163] The data comparison device 400 of the embodiment of the present invention comprises a first determination module 401, which is used to determine the unique key name of the first JSON data when the first JSON data to be compared is of the JSON array type; each object in the first JSON data includes the unique key name, and the key values ​​corresponding to the unique key names of different objects in the first JSON data are different; a second determination module 402, which is used to determine the target object corresponding to each object in the first JSON data according to the unique key name; the target object is an object in the second JSON data to be compared; and a first comparison module 403, which is used to compare each object in the first JSON data with its corresponding target object respectively. Since the comparison objects corresponding to the two JSON array type JSON data can be determined more accurately based on the unique key name, the accuracy of the comparison result of the unordered JSON array can be improved.

[0164] See also Figure 5 , Figure 5 is a structural diagram of a data comparison device provided by another embodiment of the present invention, such as Figure 5 As shown, the data comparison device 500 includes: a processor 501, a memory 502, and a computer program stored in the memory 502 and executable on the processor. The components in the data sending device 500 are coupled together via a bus interface 503. When the computer program is executed by the processor 501, the following steps are implemented:

[0165] In the case where the first JSON data to be compared is of JSON array type, determining a unique key name of the first JSON data; each object in the first JSON data includes the unique key name, and key values ​​corresponding to the unique key names of different objects in the first JSON data are different;

[0166] Determine the target object corresponding to each object in the first JSON data according to the unique key name; the target object is the object in the second JSON data to be compared;

[0167] Each object in the first JSON data is compared with its corresponding target object.

[0168] Optionally, when the computer program is executed by the processor 501, it is further used for:

[0169] Determine a unique key name of the first JSON data according to the key name and its corresponding key value in each object in the first JSON data; or

[0170] Receive a unique key name of the first JSON data specified by a user.

[0171] Optionally, the first JSON data is a first key value in a first JSON object to be compared, the second JSON data is a second key value in a second JSON object to be compared, and the key name corresponding to the first key value is the same as the key name corresponding to the second key value.

[0172] Optionally, the first JSON object is configured as a comparison reference object, and the second JSON object is configured as a compared object;

[0173] When the computer program is executed by the processor 501, it is further configured to:

[0174] In the case where the first JSON data to be compared is of a JSON array type, before determining the unique key name of the first JSON data, traverse each key name of the first JSON object, and determine whether there is a key name in the second JSON object that is the same as the key name of the first JSON object;

[0175] If so, obtain the first key value in the first JSON object and the second key value in the second JSON object respectively.

[0176] Optionally, when the computer program is executed by the processor 501, it is further used for:

[0177] In the case where the first JSON data to be compared is of a JSON array type, before determining the unique key name of the first JSON data, obtaining the first JSON string and the second JSON string;

[0178] The first JSON string and the second JSON string are respectively converted into JSON object types to obtain the first JSON object and the second JSON object.

[0179] Optionally, when the computer program is executed by the processor 501, it is further used for:

[0180] When the data type of the first key value is a single-value type, comparing the first key value with the second key value;

[0181] If the first key value is different from the second key value, a target key name, the first key value and the second key value are stored, and the target key name is the key name corresponding to the first key value or the key name corresponding to the second key value.

[0182] Optionally, when the computer program is executed by the processor 501, it is further used for:

[0183] When the data type of the first JSON data is a JSON object type, traverse each key name in the first JSON data, and determine whether there is a key name in the second JSON data that is the same as the key name in the first JSON data;

[0184] If the first key name in the first JSON data does not exist in the second JSON data, storing the first key name and the key value corresponding to the first key name;

[0185] If the second key name in the first JSON data exists in the second JSON data, obtain the third key value corresponding to the second key name in the first JSON data and the fourth key value corresponding to the second key name in the second JSON data respectively;

[0186] If the third key value is different from the fourth key value, the second key name, the third key value and the fourth key value are stored.

[0187] An embodiment of the present invention also provides a data comparison device, including a processor, a memory, and a computer program stored in the memory and executable on the processor. When the computer program is executed by the processor, the various processes of the above-mentioned data comparison method embodiment are implemented, and the same technical effect can be achieved. To avoid repetition, it will not be described here.

[0188] The embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, each process of the above-mentioned data comparison method embodiment is implemented, and the same technical effect can be achieved. To avoid repetition, it is not repeated here. The computer-readable storage medium is, for example, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0189] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or device including the element.

[0190] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present invention, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, a magnetic disk, or an optical disk), and includes a number of instructions for enabling a terminal (which can be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) to execute the methods described in each embodiment of the present invention.

[0191] The embodiments of the present invention are described above in conjunction with the accompanying drawings, but the present invention is not limited to the above-mentioned specific implementation modes, which are merely illustrative rather than restrictive. Under the guidance of the present invention, ordinary technicians in this field can also make many forms without departing from the scope of protection of the present invention and the claims, all of which are within the protection of the present invention.

Claims

1. A data comparison method, characterized in that: include: When the first JSON data to be compared is of a JSON array type, determining a unique key name of the first JSON data; Each object in the first JSON data includes the unique key name, and the key values ​​corresponding to the unique key names of different objects in the first JSON data are different; Determine the target object corresponding to each object in the first JSON data according to the unique key name; the target object is an object in the second JSON data to be compared, and the key value of the unique key name of each object in the first JSON data is the same as the key value of the unique key name of the corresponding target object; Compare each object in the first JSON data with its corresponding target object respectively; Among them, each object in the first JSON data and the target object are array elements or JSON objects.

2. The method according to claim 1, characterized in that: Determining the unique key name of the first JSON data includes: Determine a unique key name of the first JSON data according to the key name and its corresponding key value in each object in the first JSON data; or Receive a unique key name of the first JSON data specified by a user.

3. The method according to claim 1, characterized in that: The first JSON data is a first key value in a first JSON object to be compared, the second JSON data is a second key value in a second JSON object to be compared, and the key name corresponding to the first key value is the same as the key name corresponding to the second key value.

4. The method according to claim 3, characterized in that The first JSON object is configured as a comparison reference object, and the second JSON object is configured as a compared object; In the case where the first JSON data to be compared is of a JSON array type, before determining the unique key name of the first JSON data, the method further includes: Traverse each key name of the first JSON object, and determine whether there is a key name in the second JSON object that is the same as the key name of the first JSON object; If so, obtain the first key value in the first JSON object and the second key value in the second JSON object respectively.

5. The method according to claim 3, characterized in that: In the case where the first JSON data to be compared is of a JSON array type, before determining the unique key name of the first JSON data, the method further includes: Get the first JSON string and the second JSON string; The first JSON string and the second JSON string are respectively converted into JSON object types to obtain the first JSON object and the second JSON object.

6. The method according to claim 3, characterized in that The method further comprises: When the data type of the first key value is a single-value type, comparing the first key value with the second key value; If the first key value is different from the second key value, a target key name, the first key value and the second key value are stored, and the target key name is the key name corresponding to the first key value or the key name corresponding to the second key value.

7. The method according to claim 1, characterized in that The method further comprises: When the data type of the first JSON data is a JSON object type, traverse each key name in the first JSON data, and determine whether there is a key name in the second JSON data that is the same as the key name in the first JSON data; If the first key name in the first JSON data does not exist in the second JSON data, storing the first key name and the key value corresponding to the first key name; If the second key name in the first JSON data exists in the second JSON data, obtain the third key value corresponding to the second key name in the first JSON data and the fourth key value corresponding to the second key name in the second JSON data respectively; If the third key value is different from the fourth key value, the second key name, the third key value and the fourth key value are stored.

8. A data comparison device, characterized in that: include: A first determination module, configured to determine a unique key name of the first JSON data when the first JSON data to be compared is of a JSON array type; Each object in the first JSON data includes the unique key name, and the key values ​​corresponding to the unique key names of different objects in the first JSON data are different; A second determination module is used to determine the target object corresponding to each object in the first JSON data according to the unique key name; the target object is an object in the second JSON data to be compared, and the key value of the unique key name of each object in the first JSON data is the same as the key value of the unique key name of the corresponding target object; A first comparison module, used to compare each object in the first JSON data with its corresponding target object; Among them, each object in the first JSON data and the target object are array elements or JSON objects.

9. A data comparison device, characterized in that: The method comprises a processor, a memory and a computer program stored in the memory and executable on the processor, wherein the computer program implements the steps of the data comparison method according to any one of claims 1 to 7 when executed by the processor.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the data comparison method according to any one of claims 1 to 7 are implemented.

Citation Information

Patent Citations

  • Structured text comparison method and device

    CN111506608A