Javascript 从JSON模式模型派生JSON路径列表

Javascript 从JSON模式模型派生JSON路径列表,javascript,json,jsonschema,jsonpath,Javascript,Json,Jsonschema,Jsonpath,我正在寻找一个Javascript库,根据Json模式列出可能的Json路径 对于下面这样的json模式,我想列出可能的json路径 { "$id": "https://example.com/person.schema.json", "$schema": "http://json-schema.org/draft-07/schema#", "title": "Customer", "type": "object", "properties": { "firstNam

我正在寻找一个Javascript库,根据Json模式列出可能的Json路径

对于下面这样的json模式,我想列出可能的json路径

{
  "$id": "https://example.com/person.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Customer",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string",
      "description": "The person's first name."
    },
    "lastName": {
      "type": "string",
      "description": "The person's last name."
    },
    "age": {
      "description": "Age in years which must be equal to or greater than zero.",
      "type": "integer",
      "minimum": 0
    },
    "address": {
        "type": "object",
        "city": {
            "type": "string",
        },
        "country": {
            "type": "string",
        }
    }
  }
}

可能的Json路径:firstName、lastName、age、address.city和address.country

您不一定需要库。您可以使用一个简单的递归函数:

var模式={
“$id”:”https://example.com/person.schema.json",
“$schema”:”http://json-schema.org/draft-07/schema#",
“标题”:“客户”,
“类型”:“对象”,
“财产”:{
“名字”:{
“类型”:“字符串”,
“描述”:“此人的名字。”
},
“姓氏”:{
“类型”:“字符串”,
“描述”:“此人的姓氏。”
},
“年龄”:{
“说明”:“年龄(以年为单位),必须等于或大于零。”,
“类型”:“整数”,
“最小值”:0
},
“地址”:{
“类型”:“对象”,
“财产”:{
“城市”:{
“类型”:“字符串”,
},
“国家”:{
“类型”:“字符串”,
}
}
}
}
};
变量路径=({properties})=>
Object.key(properties).reduce((acc,key)=>
acc.concat(属性[键])。类型!==“对象”?键:
path(properties[key]).map(p=>`${key}.${p}`)、[]);
console.log(
路径(模式)

);基于@customcommander的回答

  • 添加对$ref的支持(防止递归)
  • 也添加层次结构父路径(即a.b.c->a+a.b+a.b.c)
  • 添加对数组项的支持(使用[]作为通用索引器)
const\u derivePathsFromSchema=(模式,属性,已定义)=>{
设路径=[];
if(!properties)返回路径;
return Object.keys(properties).reduce((path,childKey)=>{
let child=properties[childKey];
const{$ref,…childProperties}=child;
如果($ref?.startsWith('#/definitions/')){
常量定义=$ref.substr($ref.lastIndexOf('/')+1);
if(!defined.includes(definition))//防止定义的递归
{
定义。推(定义);
儿童={
…schema.definitions[定义],//加载$ref属性
…childProperties,//子属性覆盖$ref的属性
};
}
}
if(child.type==='object'){
返回path.concat(childKey,_derivePathsFromSchema(schema,child.properties,defined.slice()).map(p=>`${childKey}.${p}');
}
if(child.type=='array'&&child.items?.properties){
返回paths.concat(childKey,`${childKey}[]`,_derivePathsFromSchema(schema,child.items.properties,defined.slice()).map(p=>`${childKey}[].${p}`);
}
返回路径concat(childKey);
},路径);
};
const-derivePathsFromSchema=schema=>_-derivePathsFromSchema(schema,schema.properties,[]);
console.log(derivePathsFromSchema({
“$schema”:”http://json-schema.org/draft-07/schema#",
“定义”:{
“B类型”:{
“类型”:“对象”,
“财产”:{
“a”:{
$ref:“#/definitions/AType”
}
}
},
“AType”:{
“类型”:“对象”,
“财产”:{
“b”:{
$ref:“#/definitions/b类型”
}    
}
}
},
“类型”:“对象”,
“财产”:{
“a”:{
$ref:“#/definitions/AType”
},
“b”:{
$ref:“#/definitions/b类型”
},
“id”:{
“类型”:“字符串”
},
“数组”:{
“类型”:“数组”,
“项目”:{
“类型”:“对象”,
“财产”:{
“项”:{“类型”:“字符串”}
}
}
},
“obj”:{
“类型”:“对象”,
“财产”:{
“嵌套”:{“类型”:“字符串”}
}
}
}

}));我认为您的模式无效。
地址
的架构应该有一个
属性
关键字,该关键字包含
城市
国家