How to reconstruct Json

Tags: , , ,



I am currently having problem on modifying a Json schema, the schema is below:

{
      "$schema": "www.abc.com",
      "compounds": {
            "schemas": {
                  "Run": {
                        "type": "object",
                        "properties": {
                              "runContext": {
                                    "type": "object",
                                    "properties": {
                                          "runNumber": {
                                                "type": "integer"
                                          },
                                          "conveyanceType": {
                                                "type": "string"
                                          }
                                    },
                                    "required": [
                                          "runNumber",
                                          "conveyanceType"
                                    ]
                              }
                        }
                  }
            }
      }
}

I know this can be done using recursion, and I have tried to fetch the required item(code is below), but I have no idea how to put the required item back into properties and change to boolean value. Thanks in advance.

    private modifyJson(jsonArr: any) {
  //console.log(jsonArr);
  for (let i in jsonArr) {
    //console.log(typeof i);
    let item = jsonArr[i];
    if (typeof item === "object") {
      //console.log("here " + JSON.stringify(item));
      this.modifyJson(item);
      if (i === "required") {
        console.log("here required " + JSON.stringify(item));
        for (let j in item) {
          //console.log(item[j]);
          const required = item[j]; //fetch required item
        }
      }
    }
  }
}

I want to put it into such format:

    {
      "$schema": "www.abc.com",
      "compounds": {
            "schemas": {
                  "Run": {
                        "type": "object",
                        "properties": {
                              "runContext": {
                                    "type": "object",
                                    "properties": {
                                          "runNumber": {
                                                "type": "integer",
                                                "required": true
                                          },
                                          "conveyanceType": {
                                                "type": "string",
                                                "required": true
                                          }
                                    }
                              }
                        }
                  }
            }
      }
}

Answer

You can use recursive function with for...in loop and update object with required key on any level.

const data = {"$schema":"www.abc.com","compounds":{"schemas":{"Run":{"type":"object","properties":{"runContext":{"type":"object","properties":{"runNumber":{"type":"integer"},"conveyanceType":{"type":"string"}},"required":["runNumber","conveyanceType"]}}}}}}

function update(obj) {
  for (let prop in obj) {
    if (typeof obj[prop] === 'object') update(obj[prop]);
    if (prop === 'required') {
      obj[prop].forEach(key => {
        if (obj.properties[key]) {
          obj.properties[key].required = true;
        }
      })

      delete obj[prop]
    }
  }
}

update(data)
console.log(data)


Source: stackoverflow