Currently, I am tackling a Typescript & React project that involves taking an incoming JSON File and converting it for use with jsonforms. The structure of the input JSON is as follows:
Original Input
"availableProperties":[
{
"name":"color",
"type": "string",
"allowed_values": []
},
{
"name":"size",
"type": "string",
"allowed_values": ["small","medium","large"]
},
{
"name":"amount",
"type": "number",
"allowed_values": []
}
To utilize jsonforms, the JSON needs to be transformed into the following format:
Desired Output
properties: {
color:{
type: 'string'
},
size:{
type: 'string',
enum: ["small","medium","large"]
},
amount:{
type: 'number'
}
}
The challenge lies in iterating through the constant array in the input to create a new dictionary named "properties". Each entry in this dictionary should have a key matching the "name" property from the input, along with a nested object containing the "type" key with its corresponding value from the input. Additionally, if "allowed_values" has entries, an "enum" key with an array holding these values should be included.
I've attempted various approaches to iterate through the input but encountered obstacles in constructing the desired output structure. The input structure is defined by the interface:
interface inputProps {
name: string;
type: string;
allowed_values?: string[];
}
This interface is encapsulated within a parent interface parsed from the JsonFile:
export interface ProjectInput {
inputProperties: inputProps[];
}
Parsing the input based on this interface structure functions correctly. However, building the new JSON structure proves to be challenging.
If anyone has any suggestions or ideas, they would be greatly appreciated.
Thank you all in advance.