Skip to main content

Hello there,

 

How can we convert yaml file to json file in lwc component, in following I've created one yaml file tried to convert into json but I'm getting error saying  "Invalid reference js-yaml of type module in file yamlPractice.js", I've already installed the js-yaml module using command npm install js-yaml but still it's throwing same error again while deploying the component.

 

PFA

How to convert YAML file to JSON file in lwc component?

image (3).png 

7 respuestas
  1. 23 may 2023, 7:02

    To pass the YAML file stored in a static resource as a string to the `yamlString` parameter in the JavaScript code, you can use the `@salesforce/resourceUrl` syntax and the `fetch` API to retrieve the static resource and convert it to a string.

     

    javascript

    import { LightningElement } from 'lwc';

    import { loadScript } from 'lightning/platformResourceLoader';

    import jsYamlResource from '@salesforce/resourceUrl/jsYamlResource';

    export default class YamlToJsonConverter extends LightningElement {

    async connectedCallback() {

    try {

    await loadScript(this, jsYamlResource);

    const yamlUrl = '/resource/jsYamlResource/ymlrecord.yml';

    const response = await fetch(yamlUrl);

    const yamlString = await response.text();

    const jsonData = jsyaml.safeLoad(yamlString);

    console.log(JSON.stringify(jsonData));

    } catch (error) {

    console.error(error);

    }

    }

    }

0/9000