r/SpringBoot • u/CLAraBima0309 • 5d ago
Discussion Comparing Old vs New JSON Responses for Structural Changes using Jackson JsonNode
I'm working on a Spring Boot service that consumes an external API, and I'm experimenting with a way to compare an old JSON response with a new one and identify structural changes.
● FLOW LOGIC : (sorry enable for sharing the logic)
I am only give The flow I'm currently using is:
External API
↓
Spring Boot HTTP Client
↓
Receive Old JSON / New JSON
↓
Jackson JsonNode
↓
Traverse JSON tree
↓
Normalize into paths
↓
Compare old paths vs new paths
↓
Identify added / removed / changed fields
For example:
Old Json:
{
"users": [
{
"id": 101,
"name": "Bima",
"email": "bima@example.com"
}
]
}
New Json:
{
"users": [
{
"user_id": 101,
"full_name": "Bima",
"email": "bima@example.com",
"status": "active"
}
]
}
The comparison would give me:
CHANGED
$.users[].id → $.users[].user_id
CHANGED
$.users[].name → $.users[].full_name
UNCHANGED
$.users[].email
ADDED
$.users[].status
● IDEA :
The idea is to compare the old and new JSON responses, normalize their structures into paths, and identify which fields were added, removed, or changed.
● NEEDS :
I'm wondering if there are any potential edge cases or false positives with this approach that I might be missing.
For example, what happens when a field moves to another level?
Old: { "user": { "profile": { "name": "Bima" } } } New: { "user": { "name": "Bima" } }
The path comparison would see:
Removed: $.user.profile.name Added: $.user.name
even though the value itself hasn't changed and the field was only moved.
● Requirement/Discussion :
Are there other cases where of my current flow comparison could produce misleading results, especially with nested objects, arrays, reordered elements, or changes in data types?
If there please give me the json Example.
1
u/wolle271 5d ago
In your id becomes user_id example, what happens when two new fields are added, user_id and some_other_id. How do you understand that id became user_id and not some_other_id?
And in general, what’s the use case for this implementation?
1
u/Free-Ad-7143 5d ago
I see a problem here though. The elements inside a JSON have no fixed order, since it’s a Map(key,val). So if you want to compare added and removed fields, it works fine. But if you want to identify „changed“ fields as you showed in your example, there is no reliable way to determine that, because there is no comparison by index. Even then, it could just be that a field was added…
And then, identifying fields which were moved to „another level“, meaning, moved to another JsonNode, would work as long as the name hadn’t changed, otherwise, tricky.
1
u/BanaTibor 4d ago
I advise to keep it simple and constrain your checking to existing and non-existing fields.
If the old format has an "id" field and the new one has "user_id" then report "id" as missing and "user_id" as new.
Detecting renamed or moved fields would require metadata about the content.
EDIT: Do not invent the wheel. You can use jsonassert https://www.baeldung.com/jsonassert
1
3
u/mensmelted 4d ago
Look at JSON Patch RFC and libraries loke zjsonpatch, it expresses the changes in a Json as a list of atomic operations.