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":[]
     }]
 }
zwippie
  • 15,050
  • 3
  • 39
  • 54
Anand Soni
  • 5,070
  • 11
  • 50
  • 101

3 Answers3

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)
Ricardo
  • 667
  • 9
  • 25
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.

Sebastian N.
  • 1,962
  • 15
  • 26
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.

OhadR
  • 8,276
  • 3
  • 47
  • 53