I am working whit a restful API, written by an ex-coworker, when I use a GET to send a json from the flask API to the frontend and then call the value in the json, I get a string instead an array, the list looks like this
['ETH/BTC','LTC/BTC','BNB/BTC']
Here is what I think is relevant from the code
The route:
@bp.route('/fetch_bots_names', methods=['GET'])
def fetch_bots_names():
user_id = current_user.get_id()
bots = db.session.query(Bot).all()
viewable_bots = db.session.query(BotUserCanView).all()
user_bots = []
names = []
for x in bots:
ub = get_bot_data_as_dict(x)
if ub != None:
names.append(ub['name'])
return {'ok': True,
'msg':'Success',
'data': json.dumps(names)}, 200
The JS to fetch the data
const [botnames, setBotsNames] = useState([]);
if(savedStrats.length==0){
fetch('/auth/fetch_bots_names', {method: 'GET'})
.then(res => {return res.text()}).then(response => {
try {
let r = JSON.parse(response);
setBotsNames(r['data']);
} catch {
console.log(response);
}
});
}
and as I pointed, the botnames value is a string like the example, but I need it as an array(I think an JS array?) in order to make a drop menu whit the elements of the list, thanks in advance
jsonifyto send a response in JSON format.