Home > OS >  How to extract JSON list values from a Flask GET request?
How to extract JSON list values from a Flask GET request?

Time:09-23

I'm using a Flask app which sends a get request to an ExpressJS app running in Kubernetes. I'm having trouble extracting the list of strings that it receives:

@main.route('/')
def index():
    #latestList = requests.get('http://localhost:8085/url1').json()
    latestList = requests.get('http://express:8085/url1').json()
    request_data = []
    for i in latestList:
        request_data.append(i)
    return render_template('index.html', list_of_names = request_data)

This is the function of the ExpressJS app:

app.get('/url1', (req, res) => {
    latestList = ["name1", "name2", "name3"]
    //res.send(latestList);
    res.json(latestList);
});

What is the right way to extract the list of strings in the Flask app? I got a json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) error.

CodePudding user response:

The issue was that latestList had to be in a dict format. Changes in the Node app:

@main.route('/')
def index():
    latestList = requests.get('http://express:8085/url1').json()
    #latestList = requests.get('http://localhost:8085/url1').json()
    request_data = []
    for i in latestList:
        print(i)
        request_data.append(latestList[i])
    return render_template('index.html', list_of_places = request_data) 

In the ExpressJS app:

const app = express();
app.get('/url1', (req, res) => {
    var latestList = {1:"name1", 2:"name2", 3:"name3"}
    res.json(latestList);
});
  • Related