расшифровывать json в Zapier - PullRequest
0 голосов
/ 08 мая 2020

Я пытаюсь сделать Zapier zap, чтобы получить JSON от Olark.

Это образец JSON Olark отправляет ...

{
    "kind": "Conversation",
    "id": "EV695BI2930A6XMO32886MPT899443414",
    "tags": ["olark", "customer"],
    "items": [{
        "kind": "MessageToVisitor",
        "nickname": "John",
        "timestamp": "1307116657.1",
        "body": "Hi there. Need any help?",
        "operatorId": "1234"
    },
    {
        "kind": "MessageToOperator",
        "nickname": "Bob",
        "timestamp": "1307116661.25",
        "body": "Yes, please help me with billing."
    }],
    "visitor": {
        "kind": "Visitor",
        "id": "9QRF9YWM5XW3ZSU7P9CGWRU89944341",
        "fullName": "Bob Doe",
        "emailAddress": "bob@example.com",
        "phoneNumber": "(555) 555-5555",
        "city": "Palo Alto",
        "region": "CA",
        "country": "United State",
        "countryCode": "US",
        "organization": "Widgets Inc.",
        "ip": "123.4.56.78",
        "browser": "Chrome 12.1",
        "operatingSystem": "Windows",
        "conversationBeginPage": "http://www.example.com/path",
        "customFields": {
            "myInternalCustomerId": "12341234",
            "favoriteColor": "blue"
        },
        "chat_feedback": {
            "comments": "Very helpful, thanks",
            "friendliness": 5,
            "knowledge": 5,
            "overall_chat": 5,
            "responsiveness": 5
        }
    },
    "operators": {
        "1234": {
            "kind": "Operator",
            "id": "1234",
            "username": "jdoe",
            "nickname": "John",
            "emailAddress": "john@example.com"
        }
    },
    "groups": [{
        "name": "My Sales Group",
        "id": "0123456789abcdef",
        "kind": "Group"
    }]
}

Я могу получить то, что я хочу - электронное письмо - вот так ...

var obj = {};
var data = {};
data = JSON.parse(inputData.data);
obj.value = data.visitor["emailAddress"];
return obj;

Однако живые данные, которые поступают от Olark, являются закодированной версией JSON. Это выглядит так ...

raw_body
data=%7B%22kind%22%3A+%22Conversation%22%2C+%22id%22%3A+%224pkhSGlkBYHz0gw83L6TF0UBa6rA39Bo%22%2C+%22manuallySubmitted%22%3A+false%2C+%22items%22%3A+%5B%7B%22kind%22%3A+%22MessageToOperator%22%2C+%22nickname%22%3A+%22juliachevron%40gmail.com%22%2C+%22timestamp%22%3A+%221588965389.388434%22%2C+%22body%22%3A+%22Hi.+I+have+a+stimulator+and+it+is+no+longer+working+at+the+higher+levels%22%2C+%22visitor_nickname%22%3A+%22juliachevron%40gmail.com%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965397.901964%22%2C+%22body%22%3A+%22Hi+there%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965408.398821%22%2C+%22body%22%3A+%22Can+you+explain+more%3F%22%7D%2C+%7B%22kind%22%3A+%22MessageToOperator%22%2C+%22nickname%22%3A+%22juliachevron%40gmail.com%22%2C+%22timestamp%22%3A+%221588965445.279921%22%2C+%22body%22%3A+%22it+is+only+flashing+up+to+the+2+and+sometimes+3%22%2C+%22visitor_nickname%22%3A+%22juliachevron%40gmail.com%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965482.985280%22%2C+%22body%22%3A+%22Try+our+%5C%22oreo%5C%22+troubleshooting+test+from+page+12+of+the+User+Guide%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965488.741729%22%2C+%22body%22%3A+%22It+involves+you+pressing+the+wet+sponges+into+each+other+as+you+turn+the+device+to+the+max+setting%22%7D%2C+%7B%22kind%22%3A+%22MessageToOperator%22%2C+%22nickname%22%3A+%22juliachevron%40gmail.com%22%2C+%22timestamp%22%3A+%221588965539.836595%22%2C+%22body%22%3A+%22thank+you%21%22%2C+%22visitor_nickname%22%3A+%22juliachevron%40gmail.com%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965583.359044%22%2C+%22body%22%3A+%22This+test+is+to+make+sure+the+device+is+functioning+correctly%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965619.075057%22%2C+%22body%22%3A+%22If+you+are+not+getting+to+the+higher+levels+when+the+sponges+are+on+your+temples%2C+it+may+mean+that+you+need+to+use+more+water%2C+tighten+the+headband+a+little+more%2C+or+that+you+need+to+replace+the+sponges%22%7D%2C+%7B%22kind%22%3A+%22MessageToOperator%22%2C+%22nickname%22%3A+%22juliachevron%40gmail.com%22%2C+%22timestamp%22%3A+%221588965698.050212%22%2C+%22body%22%3A+%22it+worked.+thanks%22%2C+%22visitor_nickname%22%3A+%22juliachevron%40gmail.com%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965702.713077%22%2C+%22body%22%3A+%22You%27re+very+welcome.%22%7D%5D%2C+%22tags%22%3A+%5B%5D%2C+%22visitor%22%3A+%7B%22kind%22%3A+%22Visitor%22%2C+%22id%22%3A+%22YN7k5dd6nGH52Lvd3L6TF0VBb03B3roI%22%2C+%22fullName%22%3A+%22juliachevron%40gmail.com%22%2C+%22emailAddress%22%3A+%22juliachevron%40gmail.com%22%2C+%22ip%22%3A+%22%22%2C+%22city%22%3A+%22Oak+Park%22%2C+%22region%22%3A+%22IL%22%2C+%22country%22%3A+%22United+States%22%2C+%22countryCode%22%3A+%22US%22%2C+%22organization%22%3A+%22Comcast+Cable%22%2C+%22browser%22%3A+%22Chrome+80.0.3987.149%22%2C+%22operatingSystem%22%3A+%22Macintosh%22%2C+%22referrer%22%3A+%22https%3A%2F%2Fwww.google.com%2F%22%2C+%22conversationBeginPage%22%3A+%22https%3A%2F%2Fwww.fisherwallace.com%2F%22%2C+%22chat_feedback%22%3A+%7B%7D%7D%2C+%22operators%22%3A+%7B%22663710%22%3A+%7B%22kind%22%3A+%22Operator%22%2C+%22id%22%3A+%22663710%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22emailAddress%22%3A+%22christian%40fisherwallace.com%22%2C+%22username%22%3A+%22fisherwallace%22%7D%7D%7D

И я получаю «SyntaxError: Unexpected token d in JSON at position 0», когда пытаюсь разобрать его с помощью кода, который работает с образцом JSON .

Есть ли способ в Zapier расшифровать JSON?

Ответы [ 2 ]

1 голос
/ 09 мая 2020

Конечно, есть! Вы видите процентное кодирование . Node.js может это декодировать, но для получения действительного json вам также необходимо заменить символы + пробелами.

Попробуйте это:

const inputWithReplacedSpaces = inputData.data.replace(/\+/g, '%20') // '%20' is a space
const jsonStr = decodeURIComponent(inputWithReplacedSpaces)
const data = JSON.parse(jsonStr)
return { email: data.visitor.emailAddress }
0 голосов
/ 11 мая 2020

Было две проблемы. См. Снимок экрана.

Мне нужно было установить для данных импорта значение «data» в шаге Custom Run Javascript. А также удалите "data =" из строки в начале Raw Body.

code to unencode JSON in Zapier

...