-1

JSON: https://www.cc.puv.fi/~asa/cgi-bin/fetchOrders.py

Im trying to fetch objects and arrays within the object but im only fetching the objects.

How do i fetch also "products" arrays?

My javascript code:

fetch('https://www.cc.puv.fi/~asa/cgi-bin/fetchOrders.py')
  .then(res => res.text())
  .then(teksti => tulostus(teksti))

function tulostus(txt) {
    console.log(txt);
    let tilaukset = JSON.parse(txt);
     console.log(tilaukset);

    let a = "<table><tr><th>orderid</th><th>customerid</th><th>customer</th><th>invaddr</th><th>delivaddr</th><th>deliverydate</th><th>respsalesperson</th><th>comment</th><th>totalprice</th></tr>";
    for(let i = 0; i < tilaukset.length; i++) {
        let tilaus = tilaukset[i];
        console.log(tilaus)
        console.log(tilaus.orderid);
        a += '<tr><td>' + tilaus.orderid + '</td><td>' + tilaus.customerid + '</td><td>' + tilaus.customer + '</td><td>' + tilaus.invaddr + '</td><td>' + tilaus.delivaddr + '</td><td>' + tilaus.deliverydate + '</td><td>' + tilaus.respsalesperson + '</td><td>' + tilaus.comment + '</td><td>' + tilaus.totalprice + '</td></tr>'
    }   
    console.log(a);
    document.getElementById('info').innerHTML = a+'</table>';
}
5
  • 1
    Welcome to SO. You might find reading the site help section useful when it comes to asking a good question, and this question checklist. Code that you have worked on to solve the problem should include a minimal reproducible example, and be included in your question, and not as images. It's very difficult to debug an image. You can add code using the snippet tool [<>] in the toolbar. Commented Nov 30, 2022 at 17:43
  • 1
    .py is a sketchy/confusing extension for JSON. Valid JSON is not necessarily valid Python (If that's even what that extension means) Commented Nov 30, 2022 at 17:52
  • 2
    @Wyck that's a url, it's quite possibly responding with the header Content-Type: "application/json" Commented Nov 30, 2022 at 17:53
  • Why do you use .text instead of .json when parsing a json? Commented Nov 30, 2022 at 18:10
  • Ah I get it now. It's a python "cgi" script that is serving up the JSON. My bad. Makes sense. I think my eyes glossed over cgi-bin the first time I read it. Commented Nov 30, 2022 at 18:27

3 Answers 3

0

You likely are fetching the products array but need to add some code to iterate over it.

    for(let i = 0; i < tilaukset.length; i++) {
        let tilaus = tilaukset[i];
        console.log(tilaus)
        console.log(tilaus.orderid);
        a += '...'

        for (let j=0; j < tilaus.products.length; j++) {
          console.log(tilaus.products[i].name);
        }
    }

Replace the console log in the loop with some code to write to the html as you were doing above. Depending on how you'd like it to look at might get tricky, so consider abstracting away HTML generation or using a library that helps you componentize the ui.

Sign up to request clarification or add additional context in comments.

Comments

0
    fetch("https://www.cc.puv.fi/~asa/cgi-bin/fetchOrders.py").
        then(response => response.json())
        .then(result => display(result))

    let display = (result) => {
        for (let i = 0; i < result.length; i++) {
            let products = result[i].products 
            // here products is a Object array

            for(let i=0;i<products.length;i++){
                let product = products[i];
                console.log(product)
                // add your stuff here like product.orderid to get orderid
            }
           
        }
    }

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

you're already looping over the array and getting the object from there, if you check there's product array there in every array value(tialus.products) just save that value in a variable

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.