1

The JSON I'm receiving from this OData URL outputs in this format:

{
    "@odata.context": "URL:",
    "value": [
        {
            "@odata.etag": "W/\"JzQ0O0xJaTlFL01YY2RmYWNiSHdqeDFGMjIycFJFb0dTcXZhSkpuTXVXZDFDanM9MTswMDsn\"",
            "No": "464625",
            "Description": "4th Grade Version 2 (1.10.20)",
            "Bill_to_Customer_No": "C00020",
            "Status": "Open",
            "Person_Responsible": "",
            "Next_Invoice_Date": "0001-01-01",
            "Job_Posting_Group": "",
            "Search_Description": "4TH GRADE VERSION 2 (1.10.20)",
            "Percent_of_Overdue_Planning_Lines": 0,
            "Percent_Completed": 0,
            "Percent_Invoiced": 0,
            "Project_Manager": ""
        },
        {
            "@odata.etag": "W/\"JzQ0O3BKRUM5dkxqRlBKSTc3elIrY2FMSTZPL3RidnBablFiSUYwd0phNTEwRUk9MTswMDsn\"",
            "No": "464880",
            "Description": "Grade 1 Medium Resource Cards 7.2020",
            "Bill_to_Customer_No": "C00010",
            "Status": "Open",
            "Person_Responsible": "LINDA",
            "Next_Invoice_Date": "0001-01-01",
            "Job_Posting_Group": "",
            "Search_Description": "GRADE 1 MEDIUM RESOURCE CARDS 7.2020",
            "Percent_of_Overdue_Planning_Lines": 0,
            "Percent_Completed": 0,
            "Percent_Invoiced": 0,
            "Project_Manager": ""
        }
    ]
}

I've already been able to access the fields that I need such as Description, No, Status, etc., using the data.value[element]['field'] method:

fetch(url, {
    method: 'GET',
    credentials: 'same-origin',
    redirect: 'follow',
    agent: null,
    headers: {     
    "Content-Type": "application/json",
    'Authorization': 'Basic ' + btoa(authString),
    },
    })
    .then(response => response.json())                                  
    .then(data => {                                     
        
        console.log(data.value[0]['No']);  // this prints 464625
});

I'm struggling to figure out how to store these values locally without having to access each individual JSON element. For example:

console.log(data.value[0]['No']); // would print 464625
console.log(data.value[0]['Description']);  // would print 4th Grade Version 2 (1.10.20)
console.log(data.value[1]['No']); // would print 464800
console.log(data.value[1]['Description']);  // would print Grade 1 Medium Resource Cards 7.2020
...

Is there a way to iterate through this JSON object more effectively and store the elements locally? Thank you.

0 Answers0