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

我正在使用节点来调用drafter为了生成一个应用程序的JSON模式。 我的目标是摆脱drafter吐出的所有额外输出。 我结束了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模式规范: http : //json-schema.org/documentation.html

更新:如果你想在运行时通过节点生成模式,你可以利用像json模式生成器模块。

再次更新:我不知道我跟着你,但是从查看你的数据,你应该能够使用以下来获取application/schema+jsoncontentType的所有内容,如下所示:

 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/