omerbenamram / evtx

A Fast (and safe) parser for the Windows XML Event Log (EVTX) format
Apache License 2.0
625 stars 61 forks source link

command-line flag to replace spaces in key names with underscores for JSON output #240

Open mmguero opened 1 month ago

mmguero commented 1 month ago

Some fields in Json output have spaces in the names, e.g., .Event.EventData.Error Description. This introduces some complications when forwarding the resultant JSON in to various other tools (logstash, in my case).

I've worked out a command for jq to replace the spaces in field names with underscore, but it would be convenient to have a command-line flag to do it, producing, for example, .Event.EventData.Error_Description instead.

FWIW for anybody else who stumbles across this and wants to know how I'm doing it with jq:

evtx \
   --threads 1 \
   --format jsonl \
   --separate-json-attributes \
   "${FNAME}" 2>/dev/null | jq -c 'def walk(f):
                                    . as $in
                                    | if type == "object" then
                                        reduce keys[] as $key
                                        ( {}; . + {($key | gsub(" "; "_")): ($in[$key] | walk(f))} )
                                      elif type == "array" then
                                        map( walk(f) )
                                      else
                                        f
                                      end;
                                  walk(.)' > "${FNAME_JSON}"