获取两个 JSON 字符串的差异,忽略特定字段。

huangapple go评论77阅读模式
英文:

Get difference of two json strings by ignoring specific field

问题

我正在比较两个 JSON 字符串,并在它们不相同时获取差异。在这里,我希望在比较时忽略掉一些字段。
Jackson 是否提供了这个功能,或者有其他更好的库可用呢?

> Json1:

    {
        "name": "something",
        "id": "1",
        "version": "123"
    }

> Json2:

    {
        "name": "nothing",
        "id": "1",
        "version": "12356"
    }

我正在使用下面的代码来查找上述示例 JSON 字符串之间的差异:

    ObjectMapper mapper = new ObjectMapper();
    JsonNode node1 = mapper.valueToTree(json1);
    JsonNode node2 = mapper.valueToTree(json2);
    String diff = JsonDiff.asJson(node1, node2).toString();
    logger.info("Difference: " + diff);

但我希望在比较上述示例 JSON 时忽略 *version* 字段。

Jackson 能做到这一点吗,还是有其他更好的库可用?
英文:

I am working on comparing two JSON strings and getting the difference if not the same. Here I want to ignore a few fields while comparing.
Will Jackson provide this facility or any other library is better?

> Json1:

{
	"name": "something",
	"id": "1",
	"version": "123"
}

> Json2:

{
	"name": "nothing",
	"id": "1",
	"version": "12356"
}

I am using the below code to find the difference between the above sample json strings

ObjectMapper mapper = new ObjectMapper();
JsonNode node1 = mapper.valueToTree(json1);
JsonNode node2 = mapper.valueToTree(json2);
String diff = JsonDiff.asJson(node1, node2).toString();
logger.info("Difference: " + diff);

But I want to ignore version while comparing the JSON from the above sample JSON.

Can Jackson do this or any other better library available?

答案1

得分: 1

如果您需要现成的实现,可以参考:
https://github.com/java-json-tools/json-patch

否则,如果要求是针对简单嵌套对象,并且您希望在比较过程中忽略属性,您可以实现ObjectMapper并覆盖equals方法。
要从比较中删除文件,您可以执行:

((ObjectNode)node1).remove("version");
((ObjectNode)node2).remove("version");

然后您可以简单地调用您的JsonDiff进行分析。

英文:

If you need off-the-shelf implementation, then you can refer to:
https://github.com/java-json-tools/json-patch

Otherwise, if the requirement is on simple nested objects and you want to ignore properties during comparison you can implement ObjectMapper and override the equals method.
To Remove a file from comparison you can do:

((ObjectNode)node1).remove("version");
((ObjectNode)node2).remove("version");

Then you can simply call your JsonDiff to analyze.

答案2

得分: 1

我成功地通过使用不同的库来实现这一目标

> 导入

    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.flipkart.zjsonpatch.JsonDiff;
    import com.jayway.jsonpath.DocumentContext;
    import com.jayway.jsonpath.JsonPath;

> 代码片段

DocumentContext existingDoc = JsonPath.parse(json1);
DocumentContext incomingDoc = JsonPath.parse(json2);

existingDoc.delete("version");
incomingDoc.delete("version");

JsonNode existingNode = mapper.readTree(existingDoc.jsonString());
JsonNode incomingNode = mapper.readTree(incomingDoc.jsonString());

String diffs = JsonDiff.asJson(existingNode, incomingNode).toString();

logger.info("Difference: " + diffs);
英文:

I am successfully able to achieve it by using different libs :

> Imports

    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.flipkart.zjsonpatch.JsonDiff;
    import com.jayway.jsonpath.DocumentContext;
    import com.jayway.jsonpath.JsonPath;

> Code snippet

DocumentContext existingDoc = JsonPath.parse(json1);
DocumentContext incomingDoc = JsonPath.parse(json2);

existingDoc.delete("version");
incomingDoc.delete("version");

JsonNode existingNode = mapper.readTree(existingDoc.jsonString());
JsonNode incomingNode = mapper.readTree(incomingDoc.jsonString());

String diffs = JsonDiff.asJson(existingNode, incomingNode).toString();

logger.info("Difference: " + diffs);

huangapple
  • 本文由 发表于 2020年9月10日 17:19:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63826604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定