如何重建Json

时间:2019-06-17 10:44:12

标签: javascript json algorithm typescript

我目前在修改Json模式时遇到问题,该模式如下:

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

我知道这可以使用递归来完成,并且我尝试获取所需的项(下面的代码),但是我不知道如何将所需的项放回属性并更改为布尔值。 预先感谢。

    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
        }
      }
    }
  }
}

我想把它写成这样的格式:

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

1 个答案:

答案 0 :(得分:1)

您可以在for...in循环中使用递归函数,并在任何级别上使用required键更新对象。

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)