How to run down source of mutate error?

To understand what is happening consider

input { generator { count => 1 lines => [ '' ] } }
filter {
    mutate { add_field => { "foo1" => "" } }
    mutate { add_field => { "[foo1][bar]" => "1" } }
    mutate { add_field => { "foo" => "a" } }
    mutate { add_field => { "[foo][bar]" => "2" } }
}
output { stdout { codec => rubydebug { metadata => false } } }

That generates

[2021-09-29T14:20:46,878][WARN ][logstash.filters.mutate ][main][7b4b29026fb3b4e2ca120f9681a9c336324b1e7c46bccdc1e02cc51bfc76e3ec] Exception caught while applying mutate filter {:exception=>"Could not set field 'bar' on object '' to value '1'.This is probably due to trying to set a field like [foo][bar] = someValuewhen [foo] is not either a map or a string"}
[2021-09-29T14:20:46,886][WARN ][logstash.filters.mutate ][main][336b225f2b3d8066dd7abea8a0cd836e5705eb0605607d3262538797068ed604] Exception caught while applying mutate filter {:exception=>"Could not set field 'bar' on object 'a' to value '2'.This is probably due to trying to set a field like [foo][bar] = someValuewhen [foo] is not either a map or a string"}

So you have a field that contains an empty string and you are trying to create another field nested within. Once a field is a string you cannot use this technique to make it an object with nested fields.

The error message is badly worded ("is not either a map or a string"), missing a space in "someValuewhen", and would be a lot more useful if it logged the name of the field rather than the value. Unfortunately by the time the code is down in setChild it no longer has the name.