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
Add a ment  | 

2 Answers 2

Reset to default 5

The 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