I'm looking for a way to efficiently convert large and repetitive javascript objects into JSON strings. With the abundance of repeating property names in these objects, I want to streamline the process by replacing those names with predefined abbreviations from a mapping list. My initial idea was to utilize the replacer function within JSON.stringify to skip the step of creating the JSON string first and then manipulating it, or altering the original object's property names directly, but I have yet to find out how to do this.
For instance, considering the following mapping of property names to abbreviations:
var map = {
prop0: "p0",
prop1: "p1",
prop2: "p2"
}
I aim to transform an object like the one below:
var obj = {
prop0: "value0",
prop1: [
{prop2: "value2"},
{prop2: "value3"},
{prop2: "value4"}
]
}
into a JSON string resembling this:
{"p0":"value0","p1":[{"p2":"value2"},{"p2":"value3"},{"p2":"value4"}]}
and vice versa.