json_parse_agent.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. module Agents
  2. class JsonParseAgent < Agent
  3. include FormConfigurable
  4. cannot_be_scheduled!
  5. can_dry_run!
  6. description <<~MD
  7. The JSON Parse Agent parses a JSON string and emits the data in a new event or merge with with the original event.
  8. `data` is the JSON to parse. Use [Liquid](https://github.com/huginn/huginn/wiki/Formatting-Events-using-Liquid) templating to specify the JSON string.
  9. `data_key` sets the key which contains the parsed JSON data in emitted events
  10. `mode` determines whether create a new `clean` event or `merge` old payload with new values (default: `clean`)
  11. MD
  12. def default_options
  13. {
  14. 'data' => '{{ data }}',
  15. 'data_key' => 'data',
  16. 'mode' => 'clean',
  17. }
  18. end
  19. event_description do
  20. "Events will looks like this:\n\n %s" % Utils.pretty_print(interpolated['data_key'] => { parsed: 'object' })
  21. end
  22. form_configurable :data
  23. form_configurable :data_key
  24. form_configurable :mode, type: :array, values: ['clean', 'merge']
  25. def validate_options
  26. errors.add(:base, "data needs to be present") if options['data'].blank?
  27. errors.add(:base, "data_key needs to be present") if options['data_key'].blank?
  28. if options['mode'].present? && !options['mode'].to_s.include?('{{') && !%(clean merge).include?(options['mode'].to_s)
  29. errors.add(:base, "mode must be 'clean' or 'merge'")
  30. end
  31. end
  32. def working?
  33. received_event_without_error?
  34. end
  35. def receive(incoming_events)
  36. incoming_events.each do |event|
  37. mo = interpolated(event)
  38. existing_payload = mo['mode'].to_s == 'merge' ? event.payload : {}
  39. create_event payload: existing_payload.merge({ mo['data_key'] => JSON.parse(mo['data']) })
  40. rescue JSON::JSONError => e
  41. error("Could not parse JSON: #{e.class} '#{e.message}'")
  42. end
  43. end
  44. end
  45. end