How to serialize nested object into string? #288
-
Hi @brexhq/substation! How do I convert a nested json object into a string? Context: I am using substation to extract the relevant keys out of a json object. To keep backwards compatability with our system I then want to turn the object into a string. I have tried using the obj.to.string, but it does not work. How do I cast this correctly? ExampleAfter my transforms, I have an object like this ... (Not all the keys are known beforehand.) {"body":{"output_fields":{"evt.type":"connect","fd.lport":9125,"fd.type":"ipv6","proc.exepath":"/usr/sbin/xtables-legacy-multi","proc.pname":"kube-proxy","proc.tty":0}}} After running some transform (like {"body":"{\"output_fields\":{\"evt.type\":\"connect\",\"fd.lport\":9125,\"fd.type\":\"ipv6\",\"proc.exepath\":\"/usr/sbin/xtables-legacy-multi\",\"proc.pname\":\"kube-proxy\",\"proc.tty\":0}}"} But I get the same Thank you very much for making substation and all your hard work! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey @raphael-ru, really good question here. Under normal circumstances, we would expect this to work with a copy transform using the gjson We hadn't considered the use case of "string-ifying" values and have currently functionality that actively tries to write JSON objects at all possible times. I'm taking a look at how we can make this more selective. 👍 |
Beta Was this translation helpful? Give feedback.
@raphael-ru As of fd858a9 on the main branch
sub.tf.obj.to.string({ obj: { src: 'body', trg: 'body' } })
should work for you. Here's an example in the Substation playground CLI.