1

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

3
  • The ProjectInterface interface defines a property called inputProperties, but the input structure contains a property called availableProperties... Is that correct? Commented Mar 17, 2021 at 7:38
  • It is unclear (at least to me) what you mean by "parsing the input" based on your provided interfaces. Perhaps it's also useful to include your attempt(s) to build the desired output structure. (The description of what you intend to do is fine, but probably not sufficient here.) Commented Mar 17, 2021 at 7:40
  • And if you are able to post your attempt(s), it would also be useful to indicate why it doesn't work. Well, I mean that you should provide some error messages or unexpected output in that case. Commented Mar 17, 2021 at 7:48

1 Answer 1

2

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.

answered Mar 17, 2021 at 7:48
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I think that this could do the job indeed. I do suspect that the 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.)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.