0

In my home task, I was asked to develop Restful api on server Node.js. My problem is to convert huge XML into JSON format by using URL.

 I tried to use different directories like xml-stream, xml2json. And I found on the net a function that would do it for me, but the result was strange. I used the following guide: https://antrikshy.com/blog/fetch-xml-url-convert-to-json-nodejs

var express = require('express');
var router = express.Router();

// Parse XML to JSON.
var parseString = require('xml2js').parseString;
var https = require('https');

/* GET home page. */
router.get('/', function(req, res, next) {

  function xmlToJson(url, callback) {
    var req = https.get(url, function(res) {
      var xml = '';

      res.on('data', function(chunk) {
        xml += chunk;
      });

      res.on('error', function(e) {
        callback(e, null);
      }); 

      res.on('timeout', function(e) {
        callback(e, null);
      }); 

      res.on('end', function() {
        parseString(xml, function(err, result) {
          callback(null, result);
        });
      });
    });
  }

  var url = "https://www.boi.org.il/he/BankingSupervision/BanksAndBranchLocations/Lists/BoiBankBranchesDocs/snifim_dnld_he.xml";
xmlToJson(url, function(err, data) {
  if (err) {
    return console.err(err);
  }
  res.json(JSON.stringify(data, null, 2));
});

});

module.exports = router;

I expected a valid JSON output and got the wrong output.

"{\n  \"BRANCHES\": {\n    \"$\": {\n      \"xmlns:ns0\": \"BOI_snifim_he\"\n    },\n    \"BRANCH\": [\n      {\n        \"Bank_Code\": [\n          \"13\"\n        ],\n        \"Bank_Name\": [\n          \"13001-בנק אגוד לישראל בע\\\"מ\"\n        ],\n        \"Branch_Code\": [\n          \"142\"\n        ],\n        \"Branch_Name\": [\n          \"מבשרת\"\n        ],\n        \"Branch_Address\": [\n          \"החושן 6 9079562\"\n        ],\n        \"City\": [\n          \"מבשרת ציון\"\n        ],\n        \"Zip_Code\": [\n          \"9079562\"\n        ],\n        \"POB\": [\n          \"\"\n        ],\n        \"Telephone\": [\n          \"1-599-599-142\"\n        ],\n        \"Fax\": [\n          \"02-5706001\"\n        ],\n        \"Free_Tel\": [\n          \"\"\n        ],\n        \"Handicap_Access\": [\n          \"כן\"\n        ],\n        \"day_closed\": [\n          \"יום א\"\n        ],\n        \"Branch_Type\": [\n          \"מיוחד\"\n        ],\n        \"Date_Open\": [\n          \"02/11/2004\"\n        ],\n        \"Date_Closed\": [\n          \"26/12/2016\"\n        ],\n        \"Merge_Bank\": [\n          \"\"\n        ],\n        \"Merge_Branch\": [\n          \"\"\n        ],\n        \"X_Coordinate\": [\n          \"\"\n        ],\n        \"Y_Coordinate\": [\n          \"\"\n        ]\n      },\n      {\n        \"Bank_Code\": [\n          \"13\"\n        ],\n        \"Bank_Name\": [\n          \"13001-בנק אגוד לישראל בע\\\"מ\"\n        ],\n        \"Branch_Code\": [\n          \"123\"\n        ],\n        \"Branch_Name\": [\n          \"אגוד ישיר\"\n        ],\n        \"Branch_Address\": [\n          \"אבא הילל סלבר 13 \"\n        ],\n        

1 Answer 1

2

I expected a valid JSON output

You got valid JSON output.

See the documentation:

res.json([body])
Sends a JSON response. This method sends a response (with the correct content-type) that is the parameter converted to a JSON string using JSON.stringify().

The parameter can be any JSON type, including object, array, string, Boolean, number, or null, and you can also use it to convert other values to JSON.

res.json(null)
res.json({ user: 'tobi' })
res.status(500).json({ error: 'message' })

You're using JSON.stringify() to convert your data to a string of JSON.

Then you are passing that string to res.json() and getting a JSON encoded representation of the JSON.

Remove JSON.stringify. Then res.json() will encode the data as JSON just once.

Sign up to request clarification or add additional context in comments.

Comments

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.