V
V
Viktor Familyevich2018-03-27 09:11:37
JSON
Viktor Familyevich, 2018-03-27 09:11:37

How to parse json using Google Scripts?

Good afternoon. I want to receive messages from the live site in Google Script, the live site sends the following hook:

{
    "event_name": "chat_finished",
    "chat_id": 28806,
    "widget_id": "CzK4ffvCYC",
    "visitor": {
        "number": 804934,
        "chats_count": 2
    },
    "chat": {
        "messages": [
            {
                "timestamp": 1522049284,
                "type": "visitor",
                "message": "<Message text is not displayed here>"
            },
            {
                "timestamp": 1522049312,
                "type": "visitor",
                "message": "<Message text is not displayed here>"
            }
        ],
        "rate": null,
        "invitation": "Здравствуйте! Я могу вам чем-то помочь?"
    },
    "agents": [
        {
            "id": 99652,
            "email": "[email protected]",
            "name": "Ольга"
        }
    ],
    "session": {
        "geoip": {
            "region_code": "48",
            "country_code": "RU",
            "country": "Russian Federation",
            "region": "Moscow City",
            "city": "Moscow",
            "isp": "",
            "latitude": "55.7522",
            "longitude": "37.6156",
            "organization": "Ooo Migraph"
        },
        "utm": "campaign=(organic)|source=Google|medium=search|keyword=null|content=",
        "utm_json": {
            "campaign": "(organic)",
            "source": "Google",
            "medium": "search",
            "keyword": null,
            "content": ""
        },
        "ip_addr": "127.0.0.1",
        "user_agent": "Mozilla\/5.0 (Windows NT 5.1) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/49.0.2623.112 Safari\/537.36"
    },
    "page": {
        "url": "url",
        "title": "title"
    }
}

This post request of a slightly different kind arrives in Google Scripts itself:
{widget_id=CzK4ffvCYC, chat={rate=null, invitation=Здравствуйте! Я могу вам чем-то помочь?, messages=[Ljava.lang.Object;@fd7cb1a}, session={utm_json={campaign=(organic), source=Google, medium=search, keyword=null, content=}, geoip={country_code=RU, country=Russian Federation, city=Moscow, isp=, latitude=55.7522, organization=Ooo Migraph, region=Moscow City, region_code=48, longitude=37.6156}, ip_addr=194.88.205.37, user_agent=Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36, utm=campaign=(organic)|source=Google|medium=search|keyword=null|content=}, event_name=chat_finished, page={title=title, url=url}, visitor={number=804934, chats_count=2}, chat_id=28806, agents=[Ljava.lang.Object;@786127b6}

The problem is that incorrect arrays come to Google Scripts:
messages=[Ljava.lang.Object;@fd7cb1a}
agents=[Ljava.lang.Object;@786127b6} Can
you please tell me how to get the correct messages and agents array?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Samokhin, 2018-03-27
@Wintego

Try this
var content = JSON.parse(JSON.stringify(e.postData.contents));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question