Filtering nested json outupt

JSON output:

{"d":[{"id":960929,"created_at":"2019-06-19T00:27:28.107Z","belongs_to__application":{"__deferred":{"uri":"/resin/application(1415733)"},"__id":1415733},"is_created_by__user":{"__deferred":{"uri":"/resin/user(47100)"},"__id":47100},"commit":"9d6542006b3b9a3d61994e24f9d1c6dc","composition":{"version":"2.1","networks":{},"volumes":{"resin-data":{}},"services":{"ecoationROS":{"build":{"context":"./ROS/app/ecoation-oko-app"},"privileged":true,"restart":"always","network_mode":"host","volumes":["resin-data:/data"],"labels":{"io.balena.features.kernel-modules":"1","io.balena.features.firmware":"1","io.balena.features.dbus":"1","io.balena.features.supervisor-api":"1","io.balena.features.balena-api":"1","io.balena.update.strategy":"download-then-kill","io.balena.update.handover-timeout":""}}}},"status":"success","source":"local","start_timestamp":"2019-06-19T00:27:27.748Z","end_timestamp":"2019-06-19T00:31:24.815Z","update_timestamp":"2019-06-19T00:31:25.222Z","__metadata":{"uri":"/resin/release(960929)","type":""}},{"id":960899,"created_at":"2019-06-18T23:10:13.653Z","belongs_to__application":{"__deferred":{"uri":"/resin/application(1415733)"},"__id":1415733},"is_created_by__user":{"__deferred":{"uri":"/resin/user(47100)"},"__id":47100},"commit":"dec59d2b05c5db9a7d4d90bfaab27e3b","composition":{"version":"2.1","networks":{},"volumes":{"resin-data":{}},"services":{"ecoationROS":{"build":{"context":"./ROS/app/ecoation-oko-app"},"privileged":true,"restart":"always","network_mode":"host","volumes":["resin-data:/data"],"labels":{"io.balena.features.kernel-modules":"1","io.balena.features.firmware":"1","io.balena.features.dbus":"1","io.balena.features.supervisor-api":"1","io.balena.features.balena-api":"1","io.balena.update.strategy":"download-then-kill","io.balena.update.handover-timeout":""}}}},"status":"success","source":"local","start_timestamp":"2019-06-18T23:10:13.280Z","end_timestamp":"2019-06-18T23:27:07.857Z","update_timestamp":"2019-06-18T23:27:08.157Z","__metadata":{"uri":"/resin/release(960899)","type":""}},{"id":954099,"created_at":"2019-06-12T18:22:53.281Z","belongs_to__application":{"__deferred":{"uri":"/resin/application(1415733)"},"__id":1415733},"is_created_by__user":{"__deferred":{"uri":"/resin/user(49299)"},"__id":49299},"commit":"2200c40d0972f4fe25bba661e7945112","composition":{"version":"2.1","networks":{},"volumes":{"resin-data":{}},"services":{"ecoationROS":{"build":{"context":"./ROS/app/ecoation-oko-app"},"privileged":true,"restart":"always","network_mode":"host","volumes":["resin-data:/data"],"labels":{"io.balena.features.kernel-modules":"1","io.balena.features.firmware":"1","io.balena.features.dbus":"1","io.balena.features.supervisor-api":"1","io.balena.features.balena-api":"1","io.balena.update.strategy":"download-then-kill","io.balena.update.handover-timeout":""}}}}}]}

What I want:

[
{
   created_at: ...
   commit: ...
},
{
   created_at: ...
   commit: ...
},
{
   created_at: ...
   commit: ...
}
]

How can I approach this highly nested json? Is there anyway to easily do this in ruby? maybe a code generator or something?

Do you just want the created_at and commit fields?

@Badger, Yes, that is exactly what I'm looking for.

Currently, I'm having problems with how to approach this very nested json.

    ruby {
        code => '
            a = []
            event.get("d").each { |x|
                h = {}
                h["commit"] = x["commit"]
                h["created_at"] = x["created_at"]
                a << h
            }
            event.set("someField", a)
        '
    }

will get you

 "someField" => [
    [0] {
        "created_at" => "2019-06-19T00:27:28.107Z",
            "commit" => "9d6542006b3b9a3d61994e24f9d1c6dc"
    },
    [1] {
        "created_at" => "2019-06-18T23:10:13.653Z",
            "commit" => "dec59d2b05c5db9a7d4d90bfaab27e3b"
    },
    [2] {
        "created_at" => "2019-06-12T18:22:53.281Z",
            "commit" => "2200c40d0972f4fe25bba661e7945112"
    }
1 Like

@Badger, works like magic! I wasn't sure about the filter syntax, but now I learned, thank you!

For other people' reference:

here is the completed code with printing: Don't forget to change the field names based on your JSON output.

  ruby {
    code => '
      a = []
      event.get("d").each { |x|
          h = {}
          h["commit"] = x["commit"]
          h["created_at"] = x["created_at"]
          a << h
      }
      event.set("value", a)
    '
  }

  # prints
  ruby{
    code => 'puts event.get("value")'
  }

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.