How to write a unified diff from an old and a new text, and how to compare JSON by meaning, naming each changed value with a JSON Pointer path.
Two versions of the same thing can be compared in two honest ways, and the right one depends on what the thing is. A text file is a sequence of lines, so the comparison is a unified diff: a list of hunks that say which lines left and which arrived. A JSON document is a tree of values, so a text diff is the wrong tool: reordering two keys changes the text and changes nothing that a program can observe. For JSON, the useful answer is a list of paths, each naming one value that differs. This guide covers how to write both comparisons correctly, with small examples that were run on real files. How a finished diff can fail to apply is a separate subject, treated in our guide to why a patch isβ¦
Read the full report on AISkills402: https://aiskills402.com/blog/text-diff-and-json-diff?utm_source=devto&utm_medium=social
Top comments (0)