0

I am facing a problem regarding JSON handling in Ruby. I have one string str1 and after a few minutes it's change to str2:

str1 = 
'{   "Person1": { "state": [ {"salery": "100", "time": "100"} ],
                 "temp": [{"age": "12","time": "200" }] },
    "person2": { "state": [ {"salery": "12","time": "120"}],
                "temp": [ {"age": "12","time": "220"}]}
}'

After it's changed to:

str1 = 
'{   
      "Person1": { "state": [ {"salery": "changed", "time": "100"} ],
                        "temp": [{"age": "12","time": "200" }] },
      "person2": { "state": [ {"salery": "12","time": "120"}],
                        "temp": [ {"age": "12","time": "220"}]}
}'

Depending on this change I need output only for changing the row, because here only row with str1 -> str2 is changed ({"salery": "100" -> "salery": "changed").

output:

{
    "Person1": { "state": [ { "salery": "changed","time": "100"} ]  }
}
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Rimon
  • 119
  • 8
  • 3
    @T.J.Crowder [New hash syntax in Ruby 2.2.0](http://stackoverflow.com/questions/27759791/new-way-of-creating-hashes-in-ruby-2-2-0) i.e. {"key":"value"} – Maxim Feb 28 '15 at 10:10
  • 1
    Answer on your question is [here](http://stackoverflow.com/a/7178108/2398521). – Maxim Feb 28 '15 at 10:20
  • @ user1117381 Can we assume that `JSON.parse(str1).keys == JSON.parse(str2).keys`? – erasing Feb 28 '15 at 13:28
  • 1
    Parse the JSON strings into their respective hashes, and then use the solution on the page mentioned. – the Tin Man Feb 28 '15 at 18:48
  • @theTinMan in this link too many option, which is the real one to get the changed line output ? with these 2 hash variable str1 and str2 – Rimon Mar 09 '15 at 15:42

0 Answers0