Home > OS >  How Do I get data from JSON response
How Do I get data from JSON response

Time:08-02

I'm trying to get for example _id from this type of JSON:

[
    {
        "_id": 7,
        "baalut": "פרטי",
        "degem_cd": 558,
        "degem_manoa": "CVK",
        "degem_nm": "B8-8W2CDG",
        "horaat_rishum": 160046,
        "kinuy_mishari": "A4",
        "kvutzat_zihum": 7,
        "misgeret": "WAUZZZF46GA038023",
        "mispar_rechev": 2623638,
        "mivchan_acharon_dt": "2021-04-14T00:00:00",
        "moed_aliya_lakvish": "2016-3",
        "ramat_eivzur_betihuty": 1,
        "ramat_gimur": "LUXURY DSN",
        "rank": 0.0573088,
        "shnat_yitzur": 2016,
        "sug_degem": "P",
        "sug_delek_nm": "בנזין",
        "tokef_dt": "2022-03-08T00:00:00",
        "tozeret_cd": 19,
        "tozeret_nm": "אאודי",
        "tzeva_cd": 80,
        "tzeva_rechev": "שנהב לבן",
        "zmig_ahori": "245/35 R19",
        "zmig_kidmi": "245/35 R19"
    }
]

Overall, this JSON response is long, So I'm removing the unnecessary. This is my code:

const json = await response.json();
console.log(json.result.records);

I'm getting the result as shown before, But now I want to get data inside the JSON, So I tried to do json.result.records._id but it shows undefined

This is json.result:

{
    "_links": {
        "next": "/api/3/action/datastore_search?resource_id=053cea08-09bc-40ec-8f7a-156f0677aff3&q=2623638&limit=1&offset=1",
        "start": "/api/3/action/datastore_search?resource_id=053cea08-09bc-40ec-8f7a-156f0677aff3&q=2623638&limit=1"
    },
    "fields": [
        {
            "id": "_id",
            "type": "int"
        },
        {
            "id": "mispar_rechev",
            "info": [Object
            ],
            "type": "numeric"
        },
        {
            "id": "tozeret_cd",
            "info": [Object
            ],
            "type": "numeric"
        },
        {
            "id": "sug_degem",
            "info": [Object
            ],
            "type": "text"
        },
        {
            "id": "tozeret_nm",
            "info": [Object
            ],
            "type": "text"
        },
        {
            "id": "degem_cd",
            "info": [Object
            ],
            "type": "numeric"
        },
        {
            "id": "degem_nm",
            "info": [Object
            ],
            "type": "text"
        },
        {
            "id": "ramat_gimur",
            "info": [Object
            ],
            "type": "text"
        },
        {
            "id": "ramat_eivzur_betihuty",
            "info": [Object
            ],
            "type": "numeric"
        },
        {
            "id": "kvutzat_zihum",
            "info": [Object
            ],
            "type": "numeric"
        },
        {
            "id": "shnat_yitzur",
            "info": [Object
            ],
            "type": "numeric"
        },
        {
            "id": "degem_manoa",
            "info": [Object
            ],
            "type": "text"
        },
        {
            "id": "mivchan_acharon_dt",
            "info": [Object
            ],
            "type": "timestamp"
        },
        {
            "id": "tokef_dt",
            "info": [Object
            ],
            "type": "timestamp"
        },
        {
            "id": "baalut",
            "info": [Object
            ],
            "type": "text"
        },
        {
            "id": "misgeret",
            "info": [Object
            ],
            "type": "text"
        },
        {
            "id": "tzeva_cd",
            "info": [Object
            ],
            "type": "numeric"
        },
        {
            "id": "tzeva_rechev",
            "info": [Object
            ],
            "type": "text"
        },
        {
            "id": "zmig_kidmi",
            "info": [Object
            ],
            "type": "text"
        },
        {
            "id": "zmig_ahori",
            "info": [Object
            ],
            "type": "text"
        },
        {
            "id": "sug_delek_nm",
            "info": [Object
            ],
            "type": "text"
        },
        {
            "id": "horaat_rishum",
            "info": [Object
            ],
            "type": "numeric"
        },
        {
            "id": "moed_aliya_lakvish",
            "info": [Object
            ],
            "type": "text"
        },
        {
            "id": "kinuy_mishari",
            "info": [Object
            ],
            "type": "text"
        },
        {
            "id": "rank",
            "type": "float"
        }
    ],
    "include_total": true,
    "limit": 1,
    "q": "2623638",
    "records": [
        {
            "_id": 7,
            "baalut": "פרטי",
            "degem_cd": 558,
            "degem_manoa": "CVK",
            "degem_nm": "B8-8W2CDG",
            "horaat_rishum": 160046,
            "kinuy_mishari": "A4",
            "kvutzat_zihum": 7,
            "misgeret": "WAUZZZF46GA038023",
            "mispar_rechev": 2623638,
            "mivchan_acharon_dt": "2021-04-14T00:00:00",
            "moed_aliya_lakvish": "2016-3",
            "ramat_eivzur_betihuty": 1,
            "ramat_gimur": "LUXURY DSN",
            "rank": 0.0573088,
            "shnat_yitzur": 2016,
            "sug_degem": "P",
            "sug_delek_nm": "בנזין",
            "tokef_dt": "2022-03-08T00:00:00",
            "tozeret_cd": 19,
            "tozeret_nm": "אאודי",
            "tzeva_cd": 80,
            "tzeva_rechev": "שנהב לבן",
            "zmig_ahori": "245/35 R19",
            "zmig_kidmi": "245/35 R19"
        }
    ],
    "records_format": "objects",
    "resource_id": "053cea08-09bc-40ec-8f7a-156f0677aff3",
    "total": 1,
    "total_estimation_threshold": null,
    "total_was_estimated": false
}

CodePudding user response:

You should be able to do json.result.records[0]._id, records is an array.

  • Related