Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to call the JSON value using nodejs when the value is having hyphen

My JSON value has a value with hyphen like database-name.

I wanted to replace the value of the database-name using below code

    result.databases.database-name = 'ArangoDB';

But it is showing error, How to call the JSON value when the value is having hyphen

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is a code

const fs = require("fs");
const xml2js = require('xml2js');

fs.readFile("databases.xml", "utf-8", (err, data) => {
    if (err) {
        throw err;
    }

    xml2js.parseString(data, (err, result) => {
        if (err) {
            throw err;
        }

        result.databases.database-name = 'ArangoDB';

       
        const builder = new xml2js.Builder();
        const xml = builder.buildObject(result);

        fs.writeFile('new-databases.xml', xml, (err) => {
            if (err) {
                throw err;
            }

            console.log(`Updated XML is written to a new file.`);
        });

    });
});

>Solution :

Try to do like this:

result.databases["database-name"]

That’s the way to access properties when you have a hyphen in their key names.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading