I need help with parsing a JSON property as a Map key. The JSON string I am working with is:
const jsonString = `{
"DDBProps": {
"us-east-1": {
"test": "2",
"prod": "100"
},
"us-west-2": {
"test": "2",
"prod": "100"
}
},
"DDBProps2": {
"us-east-1": {
"test": "2",
"prod": "200"
},
"us-west-2": {
"test": "2",
"prod": "200"
}
}
}`
I want the properties like "DDBProps", "DDBProps2" to act as keys in a Map, and the values to be nested Maps. Essentially, something like
Map<string, Map<string, Map<string, string>>>
. This nested map structure will allow me to retrieve numerical values (e.g. "2", "100", "200" in this JSON) based on inputs such as: DDBPropsType, region, stage
.
To achieve this, I have declared interfaces:
interface ThroughputMap {
throughputMappings: Map<string,RegionMap>
}
interface RegionMap {
regionMappings: Map<string, StageMap>
}
interface StageMap {
stageMappings: Map<string, string>
}
// parse
const ddbScaleMap: ThroughputMap = JSON.parse(ddbScalingMappingsJson);
However, I'm facing issues with parsing the JSON into the correct structure:
console.log(ddbScaleMap)
// output below
{
"DDBProps": {
"us-east-1": {"test": "2","prod": "100"},
"us-west-2": {"test": "2","prod": "100"}
},
"DDBProps2": {
"us-east-1": {"test": "2","prod": "200"},
"us-west-2": {"test": "2","prod": "200"}
}
}
console.log(ddbScaleMap.throughputMappings) // output undefined
The problem lies in not being able to parse the property as a Map key.
Things I've attempted:
const ddbScaleMap = new Map<string, Map<string, Map<string, string>>>(Object.fromEntries(JSON.parse(jsonString)))
This method did not correctly parse the nested value. For example, calling
ddbScaleMap.get("DDBProps").get("us-west-2");
does not work as expected.