i'm currently working on a project in Typescript & React which takes an incoming JSON File and converts it to use it for jsonforms. I don't have any influence of the json structure so i have to find a way to convert it properly. The incoming Json looks like the following:
Input
"availableProperties":[
{
"name":"color",
"type": "string",
"allowed_values": []
},
{
"name":"size",
"type": "string",
"allowed_values": ["small","medium","large"]
},
{
"name":"amount",
"type": "number",
"allowed_values": []
}
as i want to use jsonforms i need this:
Output
properties: {
color:{
type: 'string'
},
size:{
type: 'string',
enum: ["small","medium","large"]
},
amount:{
type: 'number'
}
}
the input has an array which appears always with same entries. i need to iterate through that (but thats not the problem). my Idea is to create a new Dictionary called "properties" while iterating through the input i have to create a new JSON object which has the key as named in the "name" property of the input. Underneath that i need a new key with the name type which will always be the same and pick the value from the input. in addition to that, if the "allowed_values" has entries in the input then create an enum-key with an array which holds the values from the input.
I tried different approaches with iterating through the input. But i have a major issue in building that new structure. The input structure is parsed via an interface:
interface inputProps {
name: string;
type: string;
allowed_values?: string[];
}
which is hold by an parent interface which will be parsed from the JsonFile
export interface ProjectInput {
inputProperties: inputProps[];
}
parsing the input in this interface structure is working fine as mentioned. But building the new JSON Structure is not working.
Hope Somebody has an idea
Thanks to all in advance
1 Answer 1
Not sure if I got it right, but here is a suggestion.
Starting out with:
interface inputProps {
name: string;
type: string;
allowed_values?: string[];
}
interface ProjectInput {
availableProperties: inputProps[];
}
const jsonObj = `{
"availableProperties": [
{
"name": "color",
"type": "string",
"allowed_values": []
},
{
"name": "size",
"type": "string",
"allowed_values": ["small", "medium", "large"]
},
{
"name": "amount",
"type": "number",
"allowed_values": []
}
]
}`
Parsing
const myObj: ProjectInput = JSON.parse(jsonObj);
Create a new object
type KeyProps = {
type?: string;
enum?: string[];
}
type Props = {
[key: string]: KeyProps;
}
const properties: Props = {};
Loop through JSON and populate object
myObj.availableProperties.forEach(prop => {
properties[prop.name] = {};
properties[prop.name]['type'] = prop.type;
if (prop.allowed_values?.length) {
properties[prop.name]['enum'] = prop.allowed_values;
}
});
properties should match your desired output, but not quite sure if that's what you were after.
1 Comment
type property can be mandatory, but that's just a minor detail. (In the forEach callback, you can combine the first two statements into properties[prop.name] = { type: prop.type }; to avoid an error message in that case.)
ProjectInterfaceinterface defines a property calledinputProperties, but the input structure contains a property calledavailableProperties... Is that correct?