2

I am working with rails 3 & using act_as_tree. How can I iterate on nested json so that I can find difference between two nodes?

json that I want to compare looks like this :

{ "text": "To Do", "children": [{ "text": "Go jogging", "children":[] }, { "text": "Take a nap", "children":[] }] } 

and

 { "text": "To Do", "children": [{ "text": "Climb Everest", "children":[] }] } 
1

3 Answers 3

2

Please, take a look https://rubygems.org/gems/json-compare

It is possible do something to get the diff:

require 'yajl' require 'json-compare' json1 = File.new('spec/fixtures/twitter-search.json', 'r') json2 = File.new('spec/fixtures/twitter-search2.json', 'r') old, new = Yajl::Parser.parse(json1), Yajl::Parser.parse(json2) result = JsonCompare.get_diff(old, new) 
Sign up to request clarification or add additional context in comments.

Comments

1

showing the diffs - i guess you need to deserialize the JSONs into structures, and compare the objects. That seems to me the easiest way.

Comments

1

As OhadR mentioned, de-serializing and comparing the objects seems to be a viable way.

For that, check this gem:

https://github.com/liufengyun/hashdiff

It even has an online demo of comparing JSON and YAML structures, and showing the differences.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.