When I try to convert an XML file (requested from an external server) to JSON, it seems to me that xml2json does convert it however, not to a correct JSON file. Is there something that needs to be adjusted. I seem to be missing quotes for the keys.
This is my current code
app.get('/api/convertabstract/:id', async (req, res, next) => {
var data = '';
var finaldata = '';
function vertaaldata(){
return new Promise (resolve => {
https.get('https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=' + ' 11748933,11700088' +'&retmode=xml', function(res) {
if (res.statusCode >= 200 && res.statusCode < 400) {
res.on('data', function(data_) { data += data_.toString(); });
res.on('end', function() {
parser.parseString(data, function(err, result) {
finaldata = util.inspect(result, false, null, true);
}), resolve('klaar');
});
}})})
}
async function calltranslator() {
const result = await vertaaldata();
console.log(finaldata.PubmedArticleSet);
res.send('dit is de data:' + finaldata)
}
calltranslator();
});
JSON output:
{ PubmedArticleSet: {
PubmedArticle: [
{
MedlineCitation: [
{
[32m'$'[39m: { Status: [32m'MEDLINE'[39m, Owner: [32m'NLM'[39m },
PMID: [ { _: [32m'11748933'[39m, [32m'$'[39m: { Version: [32m'1'[39m } } ],
DateCompleted: [ { Year: [ [32m'2002'[39m ], Month: [ [32m'03'[39m ], Day: [ [32m'04'[39m ] } ],
DateRevised: [ { Year: [ [32m'2006'[39m ], Month: [ [32m'11'[39m ], Day: [ [32m'15'[39m ] } ],
Article: [
{
[32m'$'[39m: { PubModel: [32m'Print'[39m },
Journal: [
{
ISSN: [ { _: [32m'0011-2240'[39m, [32m'$'[39m: { IssnType: [32m'Print'[39m } } ],
JournalIssue: [....etc....
As far as I can tell, xml2js and xml2json are completely unrelated libraries. Which are you actually using - you mention both! xml2js doesn't claim to generate JSON, it claims to generate Javascript.