O
O
Oleg Kotov2018-12-04 10:35:00
JSON
Oleg Kotov, 2018-12-04 10:35:00

How to get a specific element from a JSON object?

I have a JSON string returned by the Yandex Metrics API. I need to get only one element from it - the value of the "totals" key. On pure JavaScript version, if I'm not mistaken, 1.6. (Google App Script)

json under spoiler
{
    "query": {
        "ids": [
            55027
        ],
        "dimensions": [
            "ym:s:searchEngine"
        ],
        "metrics": [
            "ym:s:users"
        ],
        "sort": [
            "-ym:s:users"
        ],
        "date1": "2017-09-01",
        "date2": "2017-09-08",
        "filters": "ym:s:startURLPath=='/kanctovary/'",
        "limit": 10000,
        "offset": 1,
        "group": "Week",
        "auto_group_size": "1",
        "quantile": "50",
        "offline_window": "21",
        "attribution": "Last",
        "currency": "RUB"
    },
    "data": [
        {
            "dimensions": [
                {
                    "name": "Google, результаты поиска",
                    "id": "google_search",
                    "favicon": "google.com"
                }
            ],
            "metrics": [
                904
            ]
        },
        {
            "dimensions": [
                {
                    "name": "Яндекс, результаты поиска",
                    "id": "yandex_search",
                    "favicon": "www.yandex.ru"
                }
            ],
            "metrics": [
                229
            ]
        },
        {
            "dimensions": [
                {
                    "name": "Мобильный Яндекс",
                    "id": "yandex_mobile",
                    "favicon": "m.yandex.ru"
                }
            ],
            "metrics": [
                60
            ]
        },
        {
            "dimensions": [
                {
                    "name": "Google: мобильное приложение",
                    "id": "google_mobile_app",
                    "favicon": "google.com"
                }
            ],
            "metrics": [
                54
            ]
        },
        {
            "dimensions": [
                {
                    "name": "Яндекс: мобильное приложение",
                    "id": "yandex_mobile_app",
                    "favicon": "www.yandex.ru"
                }
            ],
            "metrics": [
                28
            ]
        },
        {
            "dimensions": [
                {
                    "name": "Mail.ru, результаты поиска",
                    "id": "mail_search",
                    "favicon": "go.mail.ru"
                }
            ],
            "metrics": [
                11
            ]
        },
        {
            "dimensions": [
                {
                    "name": "Yahoo, результаты поиска",
                    "id": "yahoo_search",
                    "favicon": "yahoo.com"
                }
            ],
            "metrics": [
                5
            ]
        },
        {
            "dimensions": [
                {
                    "name": "Rambler, результаты поиска",
                    "id": "rambler_search",
                    "favicon": "rambler.ru"
                }
            ],
            "metrics": [
                2
            ]
        },
        {
            "dimensions": [
                {
                    "name": "Bing, результаты поиска",
                    "id": "bing_search",
                    "favicon": "bing.com"
                }
            ],
            "metrics": [
                2
            ]
        },
        {
            "dimensions": [
                {
                    "name": "Zapmeta",
                    "id": "zapmeta",
                    "favicon": "zapmeta.ru"
                }
            ],
            "metrics": [
                1
            ]
        },
        {
            "dimensions": [
                {
                    "name": "search.gmx.net",
                    "id": "search_gmx_net",
                    "favicon": "search.gmx.net"
                }
            ],
            "metrics": [
                1
            ]
        }
    ],
    "total_rows": 11,
    "total_rows_rounded": false,
    "sampled": false,
    "sample_share": 1,
    "sample_size": 1691172,
    "sample_space": 1691172,
    "data_lag": 117,
    "totals": [
        1295
    ],
    "min": [
        1
    ],
    "max": [
        904
    ]
}

I tried using the .find method, but, apparently, I don’t quite understand how it works (in JS, it’s completely zero).
I also tried using the retriever json.parse parameter, but the environment does not understand and requires closing brackets with parameters up to {

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question