I'm parsing logs from a node.js app. One of the fields is a JSON document that the app inserts in the logs. I tried to parse the JSON part in a very basic way, which appeared to work fine for a while:
json {
source => "data"
target => "node_post_data_json"
}
But the problem is with one of the subfields therein, called node_post_data_json.auth. Sometimes that field contains only a string, and Logstash will happily send it to ElasticSearch as string. Other times 'auth' contains a bunch of key/value pairs, which Logstash then parses as object. The two situations arise randomly.
The problem is, ElasticSearch throws errors if you have a field like this flip-flopping back and forth between string and object. The reason for throwing the error is described here:
I cannot force the node.js app to stop changing the type of the 'auth' field, it's out of my control. It's returned as either string or object depending on the whim of that app.
Is there a way to mutate that field based on the field's type? What is the syntax to detect the field type (either string or object)? I would like to do this:
- if the auth field is an object, leave it alone
- if the auth field is a string, convert it into an object and store the string value in a sub-field called 'string' (or some other key that doesn't conflict with the pre-exising subfields there). Or, alternatively. delete the 'auth' field and re-create it as auth_string or something.
Any suggestions are much appreciated. Thanks.