javascript – 我有一个从drafter获得的json对象,我只想要架构

我使用node来调用drafter以生成应用程序的json模式.我的目标是摆脱起草人吐出的所有额外输出.我最终得到了一个巨大的json,但我只需要它的一小部分.

这是输出:

{
  "element": "parseResult",
  "content": [
    {
      "element": "category",
      "meta": {
        "classes": [
          "api"
        ],
        "title": "Test"
      },
      "attributes": {
        "meta": [
          {
            "element": "member",
            "meta": {
              "classes": [
                "user"
              ]
            },
            "content": {
              "key": {
                "element": "string",
                "content": "FORMAT"
              },
              "value": {
                "element": "string",
                "content": "1A"
              }
            }
          }
        ]
      },
      "content": [
        {
          "element": "category",
          "meta": {
            "classes": [
              "resourceGroup"
            ],
            "title": "Questions"
          },
          "content": [
            {
              "element": "resource",
              "meta": {
                "title": "Questions"
              },
              "attributes": {
                "href": "/questions"
              },
              "content": [
                {
                  "element": "transition",
                  "meta": {
                    "title": "List All Questions"
                  },
                  "content": [
                    {
                      "element": "httpTransaction",
                      "content": [
                        {
                          "element": "httpRequest",
                          "attributes": {
                            "method": "GET"
                          },
                          "content": []
                        },
                        {
                          "element": "httpResponse",
                          "attributes": {
                            "statusCode": "200",
                            "headers": {
                              "element": "httpHeaders",
                              "content": [
                                {
                                  "element": "member",
                                  "content": {
                                    "key": {
                                      "element": "string",
                                      "content": "Content-Type"
                                    },
                                    "value": {
                                      "element": "string",
                                      "content": "application/json"
                                    }
                                  }
                                }
                              ]
                            }
                          },
                          "content": [
                            {
                              "element": "dataStructure",
                              "content": [
                                {
                                  "element": "Question List"
                                }
                              ]
                            },
                            {
                              "element": "asset",
                              "meta": {
                                "classes": [
                                  "messageBody"
                                ]
                              },
                              "attributes": {
                                "contentType": "application/json"
                              },
                              "content": "[\n  {\n    \"question\": \"Favourite programming language?\",\n    \"published_at\": \"2014-11-11T08:40:51.620Z\",\n    \"url\": \"/questions/1\",\n    \"choices\": [\n      {\n        \"choice\": \"Javascript\",\n        \"url\": \"/questions/1/choices/1\",\n        \"votes\": 2048\n      }\n    ]\n  }\n]"
                            },
                            {
                              "element": "asset",
                              "meta": {
                                "classes": [
                                  "messageBodySchema"
                                ]
                              },
                              "attributes": {
                                "contentType": "application/schema+json"
                              },
                              "content": "{\n  \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n  \"type\": \"array\"\n}"
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            },
            {
              "element": "resource",
              "meta": {
                "title": "Question"
              },
              "attributes": {
                "href": "/questions/{id}",
                "hrefVariables": {
                  "element": "hrefVariables",
                  "content": [
                    {
                      "element": "member",
                      "attributes": {
                        "typeAttributes": [
                          "required"
                        ]
                      },
                      "content": {
                        "key": {
                          "element": "string",
                          "content": "id"
                        },
                        "value": {
                          "element": "number",
                          "content": 1234
                        }
                      }
                    }
                  ]
                }
              },
              "content": [
                {
                  "element": "transition",
                  "meta": {
                    "title": "Retrieve Question"
                  },
                  "content": [
                    {
                      "element": "httpTransaction",
                      "content": [
                        {
                          "element": "httpRequest",
                          "attributes": {
                            "method": "GET"
                          },
                          "content": []
                        },
                        {
                          "element": "httpResponse",
                          "attributes": {
                            "statusCode": "200",
                            "headers": {
                              "element": "httpHeaders",
                              "content": [
                                {
                                  "element": "member",
                                  "content": {
                                    "key": {
                                      "element": "string",
                                      "content": "Content-Type"
                                    },
                                    "value": {
                                      "element": "string",
                                      "content": "application/json"
                                    }
                                  }
                                }
                              ]
                            }
                          },
                          "content": [
                            {
                              "element": "dataStructure",
                              "content": [
                                {
                                  "element": "Question"
                                }
                              ]
                            },
                            {
                              "element": "asset",
                              "meta": {
                                "classes": [
                                  "messageBody"
                                ]
                              },
                              "attributes": {
                                "contentType": "application/json"
                              },
                              "content": "{\n  \"question\": \"Favourite programming language?\",\n  \"published_at\": \"2014-11-11T08:40:51.620Z\",\n  \"url\": \"/questions/1\",\n  \"choices\": [\n    {\n      \"choice\": \"Javascript\",\n      \"url\": \"/questions/1/choices/1\",\n      \"votes\": 2048\n    }\n  ]\n}"
                            },
                            {
                              "element": "asset",
                              "meta": {
                                "classes": [
                                  "messageBodySchema"
                                ]
                              },
                              "attributes": {
                                "contentType": "application/schema+json"
                              },
                              "content": "{\n  \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n  \"type\": \"object\",\n  \"properties\": {\n    \"question\": {\n      \"type\": \"string\"\n    },\n    \"published_at\": {\n      \"type\": \"string\"\n    },\n    \"url\": {\n      \"type\": \"string\"\n    },\n    \"choices\": {\n      \"type\": \"array\"\n    }\n  },\n  \"required\": [\n    \"question\",\n    \"published_at\",\n    \"url\",\n    \"choices\"\n  ]\n}"
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          "element": "category",
          "meta": {
            "classes": [
              "dataStructures"
            ]
          },
          "content": [
            {
              "element": "dataStructure",
              "content": [
                {
                  "element": "object",
                  "meta": {
                    "id": "Question"
                  },
                  "content": [
                    {
                      "element": "member",
                      "attributes": {
                        "typeAttributes": [
                          "required"
                        ]
                      },
                      "content": {
                        "key": {
                          "element": "string",
                          "content": "question"
                        },
                        "value": {
                          "element": "string",
                          "content": "Favourite programming language?"
                        }
                      }
                    },
                    {
                      "element": "member",
                      "attributes": {
                        "typeAttributes": [
                          "required"
                        ]
                      },
                      "content": {
                        "key": {
                          "element": "string",
                          "content": "published_at"
                        },
                        "value": {
                          "element": "string",
                          "content": "2014-11-11T08:40:51.620Z"
                        }
                      }
                    },
                    {
                      "element": "member",
                      "attributes": {
                        "typeAttributes": [
                          "required"
                        ]
                      },
                      "content": {
                        "key": {
                          "element": "string",
                          "content": "url"
                        },
                        "value": {
                          "element": "string",
                          "content": "/questions/1"
                        }
                      }
                    },
                    {
                      "element": "member",
                      "attributes": {
                        "typeAttributes": [
                          "required"
                        ]
                      },
                      "content": {
                        "key": {
                          "element": "string",
                          "content": "choices"
                        },
                        "value": {
                          "element": "array",
                          "content": [
                            {
                              "element": "Choice"
                            }
                          ]
                        }
                      }
                    }
                  ]
                }
              ]
            },
            {
              "element": "dataStructure",
              "content": [
                {
                  "element": "object",
                  "meta": {
                    "id": "Choice"
                  },
                  "content": [
                    {
                      "element": "member",
                      "attributes": {
                        "typeAttributes": [
                          "required"
                        ]
                      },
                      "content": {
                        "key": {
                          "element": "string",
                          "content": "choice"
                        },
                        "value": {
                          "element": "string",
                          "content": "Javascript"
                        }
                      }
                    },
                    {
                      "element": "member",
                      "attributes": {
                        "typeAttributes": [
                          "required"
                        ]
                      },
                      "content": {
                        "key": {
                          "element": "string",
                          "content": "url"
                        },
                        "value": {
                          "element": "string",
                          "content": "/questions/1/choices/1"
                        }
                      }
                    },
                    {
                      "element": "member",
                      "attributes": {
                        "typeAttributes": [
                          "required"
                        ]
                      },
                      "content": {
                        "key": {
                          "element": "string",
                          "content": "votes"
                        },
                        "value": {
                          "element": "number",
                          "content": 2048
                        }
                      }
                    }
                  ]
                }
              ]
            },
            {
              "element": "dataStructure",
              "content": [
                {
                  "element": "array",
                  "meta": {
                    "id": "Question List"
                  },
                  "content": [
                    {
                      "element": "Question"
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

下面的位是我需要的.

content = {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "question": {
      "type": "string"
    },
    "published_at": {
      "type": "string"
    },
    "url": {
      "type": "string"
    },
    "choices": {
      "type": "array"
    }
  },
  "required": [
    "question",
    "published_at",
    "url",
    "choices"
  ]
}

这是我现在的代码,它不像我想象的那样工作.如果您需要更多信息来帮助我,请询问.

App.js

var fs = require('fs');
var edit = require('string-editor');
var lodash = require('lodash');
var _ = require('underscore');
const util = require('util');
const exec = require('child_process').exec;
exec('drafter -f json test.apib' , function(error, stdout, stderr) {
    const json = JSON.parse(stdout);
    //console.log(json)
var res
function loopThrough(obj){
        for(var key in obj){
            if(!obj.hasOwnProperty(key)) continue;
            if(typeof obj[key] !== 'object'){
                //if (cond) var x = {'$schema': };
                //if (_.hasIn(obj, '$schema')) {

                    res = res + "\n" + (key+" = "+obj[key]);
                //}
            } else {
                loopThrough(obj[key]);
            }
        }
    }
    loopThrough(json);
    //parse = JSON.parse(test);
    //string = JSON.stringify(test, null,  '  ');
   //string = string.replace(/\\n/g, '');
    fs.writeFile('test.json', res, function(err) {
    if(err) {
        return console.log(err);
    }
    console.log("The file was saved!");
    });  
    if (error !== null) {
        console.log('exec error: ' + error);
    }

});

这是我要承担的输出.

undefined
element = parseResult
element = category
0 = api
title = Test
element = member
0 = user
element = string
content = FORMAT
element = string
content = 1A
element = category
0 = resourceGroup
title = Questions
element = resource
title = Questions
href = /questions
element = transition
title = List All Questions
element = httpTransaction
element = httpRequest
method = GET
element = httpResponse
statusCode = 200
element = httpHeaders
element = member
element = string
content = Content-Type
element = string
content = application/json
element = dataStructure
element = Question List
element = asset
0 = messageBody
contentType = application/json
content = [
  {
    "question": "Favourite programming language?",
    "published_at": "2014-11-11T08:40:51.620Z",
    "url": "/questions/1",
    "choices": [
      {
        "choice": "Javascript",
        "url": "/questions/1/choices/1",
        "votes": 2048
      }
    ]
  }
]
element = asset
0 = messageBodySchema
contentType = application/schema+json
content = {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "array"
}
element = resource
title = Question
href = /questions/{id}
element = hrefVariables
element = member
0 = required
element = string
content = id
element = number
content = 1234
element = transition
title = Retrieve Question
element = httpTransaction
element = httpRequest
method = GET
element = httpResponse
statusCode = 200
element = httpHeaders
element = member
element = string
content = Content-Type
element = string
content = application/json
element = dataStructure
element = Question
element = asset
0 = messageBody
contentType = application/json
content = {
  "question": "Favourite programming language?",
  "published_at": "2014-11-11T08:40:51.620Z",
  "url": "/questions/1",
  "choices": [
    {
      "choice": "Javascript",
      "url": "/questions/1/choices/1",
      "votes": 2048
    }
  ]
}
element = asset
0 = messageBodySchema
contentType = application/schema+json
content = {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "question": {
      "type": "string"
    },
    "published_at": {
      "type": "string"
    },
    "url": {
      "type": "string"
    },
    "choices": {
      "type": "array"
    }
  },
  "required": [
    "question",
    "published_at",
    "url",
    "choices"
  ]
}
element = category
0 = dataStructures
element = dataStructure
element = object
id = Question
element = member
0 = required
element = string
content = question
element = string
content = Favourite programming language?
element = member
0 = required
element = string
content = published_at
element = string
content = 2014-11-11T08:40:51.620Z
element = member
0 = required
element = string
content = url
element = string
content = /questions/1
element = member
0 = required
element = string
content = choices
element = array
element = Choice
element = dataStructure
element = object
id = Choice
element = member
0 = required
element = string
content = choice
element = string
content = Javascript
element = member
0 = required
element = string
content = url
element = string
content = /questions/1/choices/1
element = member
0 = required
element = string
content = votes
element = number
content = 2048
element = dataStructure
element = array
id = Question List
element = Question

最佳答案 您可以尝试通过在线JSON模式生成工具运行它,如下所示:
http://jsonschema.net/#/

您还需要考虑阅读建议的JSON Schema规范:http://json-schema.org/documentation.html

更新:如果您想通过Node在运行时生成模式,您可以利用像json-schema-generator这样的模块.

再次更新:我不确定我是否关注您,但是,通过查看您的数据,您应该能够使用以下内容来获取applicationType / schema json的contentType的所有内容,如下所示:

var _ = require('lodash');
var schemas = [];

function isSchemaType(contentItem) {
  return _.get(contentItem, 'attributes.contentType') == 'application/schema+json';
}

function parseContent(content) {
  if (_.isObject(content) && _.isArray(content.content)) {
    _.forEach(content.content, parseContent);
  } else if (isSchemaType(content)) {
    schemas.push(JSON.parse(content.content));
  }
}

parseContent(jsonData);

console.log(schemas);

这是一个工作的jsfiddle:https://jsfiddle.net/k7dcd6s2/

点赞