0

it seems no matter what i do with the result it even as it comes back as an object because when I console.log it it prints [object object] however when I check it's typeof it's always a string, no matter what I do, I am trying to do JSON.parse immediately it breaks as it's already an object, just not identified as one for some reason.. if I do stringify and then parse it still stays a string as well.

This is what I get back: { "PPR": "Some text", "DDA": "another text" }

 var rp = require('request-promise');

 function myff(input, callback) {

 const URL = "https://test.com";


 try{
    var options = {
        method: 'GET',
        uri: URL,
        headers:{
          'Content-Type': 'application/json',
          'key': 'xxff'
        },
    };

    rp(options)
        .then(function (parsedBody) {
            var a = parsedBody;
            console.log("ParsedBody: " +  a);
            console.log("ParsedBody type : " + typeof a);

               var stringy = JSON.stringify(parsedBody);
               var parsy = JSON.parse(stringy);

               console.log("type stringy: " + typeof stringy);
               console.log("type parsy: " + typeof parsy);


            callback(null, JSON.parse(parsedBody));

        })
        .catch(function (err) {
            console.log(err)
        });

 }catch (e){
     console.log(" erros:" + e);
 }
 }
makat
  • 154
  • 1
  • 3
  • 14
  • What do you see exactly when you do `console.log("ParsedBody type : " + typeof parsedBody);` ? – goto Apr 16 '20 at 19:42
  • Use a comma in your console log. Using a plus sign is string concatenation, hence the reason you're getting [object Object] which is the string representation of any js object. – Phix Apr 16 '20 at 19:48

1 Answers1

1

You just need to parse the returned JSON response. Use comma operator inside console statement as (+) causes concatenation and you will not be able to see the right result.

var rp = require('request-promise');

 function myff(input, callback) {

 const URL = "https://test.com";


 try{
    var options = {
        method: 'GET',
        uri: URL,
        headers:{
          'Content-Type': 'application/json',
          'key': 'xxff'
        },
    };

    rp(options)
        .then(function (parsedBody) {
            var a = JSON.parse(parsedBody);
            console.log("ParsedBody: ",  a);
            console.log("ParsedBody type : ", typeof a);

            callback(null, a);

        })
        .catch(function (err) {
            console.log(err)
        });

 }catch (e){
     console.log(" erros:" + e);
 }
 }

I hope it will help you.

Jasdeep Singh
  • 7,901
  • 1
  • 11
  • 28