Guest User

Untitled

a guest
Jul 16th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. key = "id", value = 1
  2. key = "mem/stat1", value = 10
  3. key = "more_stats/extra_stats/stat7", value = 5
  4.  
  5. my_json =
  6. {
  7. "id": 1,
  8. "system_name": "System_1",
  9. "mem" : {
  10. "stat1" : 10,
  11. "stat2" : 1056,
  12. "stat3" : 10563,
  13. },
  14. "other_stats" : {
  15. "stat4" : 1,
  16. "stat5" : 2,
  17. "stat6" : 3,
  18. },
  19. "more_stats" : {
  20. "name" : "jdlfjsdlfjs",
  21. "os" : "fjsalfjsl",
  22. "error_count": 3
  23. "extra_stats" : {
  24. "stat7" : 5,
  25. "stat8" : 6,
  26. },
  27. }
  28. }
  29.  
  30. def hashkeys(json, keys = [], result = [])
  31. if json.is_a?(Hash)
  32. json.each do |key, value|
  33. hashkeys(value, keys + [key], result)
  34. end
  35. else
  36. result << keys
  37. end
  38. result.join("/")
  39. end
  40.  
  41. id/system_name/mem/stat1/mem/stat2/...
  42.  
  43. nested_key_value(my_json)
  44. some logic loop involving key and value:
  45. send_info(final_key, final_value)
Add Comment
Please, Sign In to add comment