1

I need to convert some JSON to XML with this library, I need to do that in order to send some data to the Database in a post request I am working on.

This is what req.body returns

{ DealerName: 'amrcl',
  CardId: '123',
  Nickname: 'mkm123',
  Picture: 'http://lorempixel.com/150/150/',
  Active: '1',
  LegalId: '1',
  TypeId: '1'
}

is dynamic data. Let me show you the code

export default function (req, res) {
  try {
    const connection = new sql.Connection(spConfig, function spConnection (errSpConnection) {
      const request = connection.request();
      if (errSpConnection) {
        res.status(401);
      }
      request.input('Dealer_Param', sql.VarChar(1000), req.body);
      request.input('param_IS_DEBUG', sql.Bit, null);
      request.output('output_IS_SUCCESSFUL', sql.Bit);
      request.output('output_STATUS', sql.VarChar(500));
      request.execute('[mydbo].[StoredProcedure]', function spExecution (errSpExecution, dataset) {
        connection.close();
        if (errSpExecution) {
          res.status(401);
        } else {
          if (request.parameters.output_IS_SUCCESSFUL.value) {
            res.status(200).json({
              success : 'New dealer successfully inserted.',
            });
          }
        }
      });
    });
  }
}

that is for Stored Procedure method which is with the mssql module.

As you see the code above, there is a failure error, because in request.input('Dealer_Param', sql.VarChar(1000), req.body); I am sending the JSON I paste at the beginning of the question. But if instead of req.body I put this XML with Dummy data '<Dealers><Detail DealerName = "TESTING123" CardId = "1222" NickName = "tester123" Active = "1" LegalId = "16545" TypeId = "1"></Detail></Dealers>' then everything works fine because the DB need to receive an XML.

So, what are your recommendations, what should I do to put the JSON data as a XML ?

4
  • 1
    So just npm install that library and use it there right? Then you can do json2xml(req.body)? Commented Oct 10, 2015 at 0:29
  • Have you got the solution of your problem? Commented Feb 17, 2018 at 3:28
  • @AmitGupta see the accepted answer by @ accraze below Commented Feb 21, 2018 at 21:54
  • grt. You can also have a look on fast-xml-parser for the same Commented Feb 22, 2018 at 4:06

1 Answer 1

2

I would just load the json2xml library...

  • install $ npm install json2xml

  • import the module in your code: var json2xml = require("json2xml");

and then convert the json to xml like so:

var key,
    attrs=[];

for (key in req.body) {
    if (req.body.hasOwnProperty(key)) {
        var obj = {};
        obj.key = req.body.key;
        attrs.push(obj);
    }
}

var dealerXml = json2xml({dealer:req.body, attr:attrs}, { attributes_key:'attr'}); 

request.input('Dealer_Param', sql.VarChar(1000), dealerXml);
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.