1

I've been trying for several days but I can't do the diel json parser at this url http://sdmx.istat.it/SDMXWS/rest/dataflow/IT1//?detail=full&references=none&format=jsonstructure

I can write it to a file and if I try to upload the file to one of the many online tools it tells me that it is valid.

Also the url test in the same online tools gives me valid json. this is my code what am I wrong?

    var express = require('express');
var router = express.Router();
const fetch = require('node-fetch');


const JSONstat = require("jsonstat-toolkit");
const JSONstatUtils = require("jsonstat-suite");
const fs = require('fs');

router.get('/', async(req, res) => {
    
    var ws_entry_point ='http://sdmx.istat.it/SDMXWS/rest';
    var resource = 'dataflow';
    var agencyID ='IT1';
    var detail = 'full';
    var references = 'none';
    var format = 'jsonstructure';

    var queryUrl = ws_entry_point + '/' + resource + '/' + agencyID + '//?detail=' + detail + '&references=' + references + '&format=' + format;
    //http://sdmx.istat.it/SDMXWS/rest/dataflow/IT1//?detail=full&references=none&format=jsonstructure
    
    console.log( queryUrl );
    fetch(queryUrl, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json, charset=UTF-8'
      }}).then(checkResponseStatus)
      .then(async response => {
      try {
       const data = await response.json()
       console.log('response data?', data)
     } catch(error) {
       console.log('Error happened here!')
       console.error(error)
     }
    }).catch(err => console.log(err));

//    fs.writeFile('dataflow.json', res.data, function (err) {
//      if (err) return console.log(err);
//      
//      console.log('write response.text > dataflow.json');
//      fs.readFile('dataflow.json', 'utf8', function (err, dataF) {
//        if (err) throw err; // we'll not consider error handling for now
//        console.log('read  < dataflow.json');
//        //var obj = JSON.parse(dataF);
//        console.log(dataF);
//      });
//    });

  });
  function checkResponseStatus(res) {
    if(res.ok){
        return res
    } else {
        throw new Error(`The HTTP status of the reponse: ${res.status} (${res.statusText})`);
    }
  }

any help is appreciated, regards, Maurizio

1 Answer 1

2

The server does not respond with the requested application/json content-type, instead it sends a response with content-type application/vnd.sdmx.structure+json and a response content that contains a json-like string but with some incorrect whitespaces.

You can fix this by using .text() on the response and manually parsing the content after trimming it:

const rawData = await response.text();
const data = JSON.parse(rawData.trim());
console.log('response data?', data)
Sign up to request clarification or add additional context in comments.

5 Comments

.trim () was what I was missing! I feel like an idiot I had already tried the response.text () paser many times but without the trim I always had the same error "SyntaxError: Unexpected token in JSON at position" thank you very much
Happy to help, please accept my answer by clicking the checkmark on the left if this solved your issue, thanks!
yes it is the solution for me, I should have accepted your solution
I did it "You last voted on this answer yesterday. Your vote is now locked in unless this answer is edited."
Yes, thanks for the upvote, but there also checkmark symbol you can click to accept the answer - un segno di spunta in italiano.. You may want to read meta.stackexchange.com/questions/5234/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.