比较两个具有相同嵌套结构和相同键但值可以不同的 json?
Compare two json which has same nested structure and same keys but values can be different?
例如:
Json A =>
{
"customer": {
"age": 29,
"fullName": "Emily Jenkins",
"year":1988,
"address":{
"lineOne":"lineOne",
"lineTwo":"lineTwo"
}
},
"handset":{
"phone":"0123456789",
"colour":"black"
}
}
Json B =>
{
"customer": {
"fullName": "Sammy J",
"age": 31,
"year":1985,
"address":{
"lineTwo":"lineTwo",
"lineOne":"lineOne"
}
},
"handset":{
"colour":"red",
"phone":"0123456788"
}
}
我想比较这两个 json,它应该 return 正确,因为键匹配并且两个 json 结构相同。那么有没有一种干净的方法呢?
我知道使用 Gson lib 我可以比较两个 JsonElement 但这也会匹配值,我不想保留这些值作为我的用例的约束。
JsonParser parser = new JsonParser();
JsonElement p1 = parser.parse(JsonA);
JsonElement p2 = parser.parse(JsonB);
System.out.printf(p1.equals(p2) ? "Equal" : "Not Equal"); //it returns true if values are same.
这确实是 JSON 模式验证的情况,请参阅 Core and Validation。
有几个 JSON 模式验证器可用,对于下面的示例,我使用 enter link description here . The library binaries are available in Maven, it can also be downloaded (along with all dependency and javadoc) from here . Other validators are also available from here。
通过 this guide.
也许会有帮助
示例的架构如下 (SampleJsonSchema.json):
{
"$schema":"http://json-schema.org/draft-07/schema#",
"id":"http://test.org/sampleJsonSchema1",
"title":"SampleJSONSchema",
"description":"This is the description",
"type":"object",
"properties":{
"customer":{
"type":"object",
"properties":{
"fullname":{"type":"string"},
"age":{"type":"integer"},
"year":{"type":"integer"},
"address":{
"type":"object",
"properties":{
"lineOne":{"type":"string"},
"lineTwo":{"type":"string"}
},
"required":["lineOne", "lineTwo"],
"maxProperties":2
}
},
"required":["fullname","age","year","address"],
"maxProperties":4
},
"handset":{
"type":"object",
"properties":{
"phone":{"type":"string"},
"colour":{"type":"string"}
},
"required":["phone","colour"],
"maxProperties":2
}
}
}
使用的数据如下(SampleJsonData.json:
{
"customer": {
"fullname": "Emily Jenkins",
"age": 29,
"year":1988,
"address":{
"lineOne":"lineOne",
"lineTwo":"lineTwo"
}
},
"handset":{
"phone":"0123456789",
"colour":"black"
}
}
验证例程如下:
包 org.test;
import java.io.File;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
public class JsonValidation {
public static void main(String[] args) throws Exception{
String jsonSchemaPath="F:\workspaces\utilities\TestJava\jsonSchema\SampleJsonSchema.json";
String jsonDataPath="F:\workspaces\utilities\TestJava\jsonSchema\SampleJsonData.json";
ObjectMapper mapper=new ObjectMapper();
JsonNode schemaNode=mapper.readValue(new File(jsonSchemaPath), JsonNode.class);
JsonNode dataNode=mapper.readValue(new File(jsonDataPath), JsonNode.class);
JsonSchema jsonSchema=JsonSchemaFactory.byDefault().getJsonSchema(schemaNode);
ProcessingReport validationReport=jsonSchema.validate(dataNode);
System.out.println(validationReport.toString());
}//main closing
}//class closing
通过更改架构和数据以观察不同的结果,可以运行进行一些测试。
例如:
Json A =>
{
"customer": {
"age": 29,
"fullName": "Emily Jenkins",
"year":1988,
"address":{
"lineOne":"lineOne",
"lineTwo":"lineTwo"
}
},
"handset":{
"phone":"0123456789",
"colour":"black"
}
}
Json B =>
{
"customer": {
"fullName": "Sammy J",
"age": 31,
"year":1985,
"address":{
"lineTwo":"lineTwo",
"lineOne":"lineOne"
}
},
"handset":{
"colour":"red",
"phone":"0123456788"
}
}
我想比较这两个 json,它应该 return 正确,因为键匹配并且两个 json 结构相同。那么有没有一种干净的方法呢?
我知道使用 Gson lib 我可以比较两个 JsonElement 但这也会匹配值,我不想保留这些值作为我的用例的约束。
JsonParser parser = new JsonParser();
JsonElement p1 = parser.parse(JsonA);
JsonElement p2 = parser.parse(JsonB);
System.out.printf(p1.equals(p2) ? "Equal" : "Not Equal"); //it returns true if values are same.
这确实是 JSON 模式验证的情况,请参阅 Core and Validation。
有几个 JSON 模式验证器可用,对于下面的示例,我使用 enter link description here . The library binaries are available in Maven, it can also be downloaded (along with all dependency and javadoc) from here . Other validators are also available from here。
通过 this guide.
也许会有帮助示例的架构如下 (SampleJsonSchema.json):
{
"$schema":"http://json-schema.org/draft-07/schema#",
"id":"http://test.org/sampleJsonSchema1",
"title":"SampleJSONSchema",
"description":"This is the description",
"type":"object",
"properties":{
"customer":{
"type":"object",
"properties":{
"fullname":{"type":"string"},
"age":{"type":"integer"},
"year":{"type":"integer"},
"address":{
"type":"object",
"properties":{
"lineOne":{"type":"string"},
"lineTwo":{"type":"string"}
},
"required":["lineOne", "lineTwo"],
"maxProperties":2
}
},
"required":["fullname","age","year","address"],
"maxProperties":4
},
"handset":{
"type":"object",
"properties":{
"phone":{"type":"string"},
"colour":{"type":"string"}
},
"required":["phone","colour"],
"maxProperties":2
}
}
}
使用的数据如下(SampleJsonData.json:
{
"customer": {
"fullname": "Emily Jenkins",
"age": 29,
"year":1988,
"address":{
"lineOne":"lineOne",
"lineTwo":"lineTwo"
}
},
"handset":{
"phone":"0123456789",
"colour":"black"
}
}
验证例程如下:
包 org.test;
import java.io.File;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
public class JsonValidation {
public static void main(String[] args) throws Exception{
String jsonSchemaPath="F:\workspaces\utilities\TestJava\jsonSchema\SampleJsonSchema.json";
String jsonDataPath="F:\workspaces\utilities\TestJava\jsonSchema\SampleJsonData.json";
ObjectMapper mapper=new ObjectMapper();
JsonNode schemaNode=mapper.readValue(new File(jsonSchemaPath), JsonNode.class);
JsonNode dataNode=mapper.readValue(new File(jsonDataPath), JsonNode.class);
JsonSchema jsonSchema=JsonSchemaFactory.byDefault().getJsonSchema(schemaNode);
ProcessingReport validationReport=jsonSchema.validate(dataNode);
System.out.println(validationReport.toString());
}//main closing
}//class closing
通过更改架构和数据以观察不同的结果,可以运行进行一些测试。