Search code examples
javascriptjqueryjsonapigoogle-visualization

jQuery nested query to JSON API with google charts


I have this JSON from an API and I want to get "Value" and "Date" from the data only from the first "Datos".

The problem is that it is only taking the first figures from each "Datos" with [Data[0].Date], [Data[0].Value] (code below). How can I do to get all the values only from the first one and the second separately.

"Datos":[
    {
        "Date":"2020-11-01",
        "Value":100
    },
    {
        "Date":"2020-10-01",
        "Value":101
    },
    {
        "Date":"2020-09-01",
        "Value":102
        
    },
]
"Datos":[
    {
        "Date":"2020-11-01",
        "Value":1%
    },
    {
        "Date":"2020-10-01",
        "Value":2%
    },
    {
        "Date":"2020-09-01",
        "Value":3%
        
    },
]

I'm using this code:

<script>
    function drawLineChart() {
        $.ajax({
            url:         "url",
            dataType:    "json",
            type:        "GET",
            contentType: "application/json; charset=utf-8",
            success:     function (data) {
                var arrSales = [ [ 'Month', 'Sales Figure' ] ];
    
                $.each(data, function (index, value) {
                    arrSales.push([ value.Datos[0].Date, value.Datos[0].Value ]);
                });
            }
        })
    }
</script>

Thank you!!


Solution

  • this should get all the data from the first Datos in data...

    function drawLineChart() {
        $.ajax({
            url:         "url",
            dataType:    "json",
            type:        "GET",
            contentType: "application/json; charset=utf-8",
            success:     function (data) {
                var arrSales = [ [ 'Month', 'Sales Figure' ] ];
    
                if (data.length > 0) {
                  $.each(data[0].Dataos, function (index, value) {
                      arrSales.push([ value.Date, value.Value ]);
                  });
                }
            }
        })
    }