Forum Academy Marketplace Showcase Pricing Features

Expected a string, but got a number : How to fix?

Hi, I am trying to fix this error that occurs when returning an array that has both strings and numbers in it. My working script is

const response = context.request(options);
	console.log(response);

The model we will use 
{
  "mins": 5,
  "price": "34823.91104827"
}

*/

var body = JSON.parse(response.body);

const {convert} = require('json-to-bubble-object');

body.mins = body.mins.toString()
        
let returnlist = []
returnlist.push(convert(body))
                
    return {
        "result": returnlist
    
    }
}

which returns data because the body.mins field is a number which is converted to a string. I want to try and write a loop that will convert all calls with object fields that have numbers and change them to strings.

I am trying this script but it is not working for some reason

const response = context.request(options);
	console.log(response);
    

/* 

The model we will use 
{
  "mins": 5,
  "price": "34823.91104827"
}

*/

var body = JSON.parse(response.body);

const {convert} = require('json-to-bubble-object');                        

var bodystr = body;
    
for (let key in body){
    if (typeof body.key == "number")
    bodystr.key = body.key.toString()
} 
    let returnlist = []
returnlist.push(convert(bodystr))
           
    return {
        "result": returnlist
    
    }
}

Does anyone know how to get around the error?

![image|690x151](upload://w0PnOLcdbBZpfXYjeRwpYzBrxCr.png)

I got a solution of iterating through the object literal using the following code, hope this helps other people who come looking. my problem was I was using the .toString() function rather than JSON.stringify

var bodystr = function(body){
  Object.keys(body).forEach(function(key){ 
      if (typeof body[key] == "number"){
      body[key] = JSON.stringify(body[key]) }
  		else 
  		{body[key] = body[key] }});
  return body;
}
1 Like