admin 管理员组文章数量: 1086019
Am using swagger-ui, which needs input as in object type.
I was trying JSON.parse
the YAML response and am getting the below
VM9790:1 Uncaught SyntaxError: Unexpected token s in JSON at position 0
at JSON.parse (<anonymous>)
Please suggest the right way to parse the YAML response to an object
Response: .yaml
Am using swagger-ui, which needs input as in object type.
I was trying JSON.parse
the YAML response and am getting the below
VM9790:1 Uncaught SyntaxError: Unexpected token s in JSON at position 0
at JSON.parse (<anonymous>)
Please suggest the right way to parse the YAML response to an object
Response: https://raw.githubusercontent./openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml
Share Improve this question edited May 12, 2020 at 13:49 palaѕн 74k17 gold badges122 silver badges139 bronze badges asked May 12, 2020 at 13:47 LokeshLokesh 691 gold badge1 silver badge12 bronze badges 1- 1 yaml is not json :/ your need a yaml parser – Lawrence Cherone Commented May 12, 2020 at 13:56
2 Answers
Reset to default 5The easiest way to parse a YAML file or response is to use a library for that purpose. You can try https://github./eemeli/yaml, which seems fairly well maintained at the time of writing this answer.
Install the library in your project:
npm install yaml
Example usage:
import YAML from 'yaml'
// or
const YAML = require('yaml')
YAML.parse(yamlGoesHere)
Use parseDocument.
package.json
"yaml": "^2.1.3"
import * as yaml from 'yaml';
/*
Other code
*/
const document = yaml.parseDocument(yourYamlFile);
console.log(document.get('yourKey'));
本文标签: jsonParse YAML response to objectJavascriptStack Overflow
版权声明:本文标题:json - Parse YAML response to object - Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744029544a2521244.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论