Zapier identifying JSON data as string - json

My question is how can I pull the values for events.payload.media.name?
I am posting to a raw zapier webhook from another app. If I check it using requestb.in it comes through as "Content-Type: application/json". The output is also validating as JSON.
{
"hook":{
"uuid":"1asdfasd5-asdf-4f52-bd31-c7a544897808"
},
"events":[
{
"uuid":"0asdfasdfasdf0",
"type":"viewing_session.turnstile.converted",
"payload":{
"visitor":{
"id":"28b606b_7853753-3868-4f07-9543-70da084452cc-7442322af-407bdc31d8fc-2739"
},
"viewing_session":{
"id":"154284_b40c5358-1faf-40e9-a44e-60aa641a11cd-fd3c69d8d-302471c603f4-8245"
},
"name":null,
"media":{
"url":"https://things.wistia.com/medias/asdfasdf",
"thumbnail":{
"url":"http://embed.wistia.com/deliveries/asdfasdfasdfasdfasdfasdfasd.jpg?image_crop_resized=200x120"
},
"name":"this is what I want!",
"id":"asdfasdfasdf",
"duration":52.872
},
"last_name":null,
"foreign_data":{
},
"first_name":null,
"email":"email#email.com"
},
"metadata":{
"account_id":"asdfasdfasdf"
},
"generated_at":"2017-05-02T07:31:08Z"
}
]
}
However, when I check the typeof data in the output it is telling me that it is a string (see my code to check below). This prevents me from pulling the info out of it using:
return {stuff: typeof inputData.thing.events.payload.media.name};
I'm a huge noob, am I missing something fundamental here?
screenshot to check typeof data

events is an array, so you would access it like this:
inputData.thing.events[0].payload.media.name

is there a way to have the whole payload without creating a new App in Zapier? inputData didn't work

Related

Best Schema for a Data List in JSON for RestFul API to use in Angular

I've been wondering for some days what kind of scheme would be more appropriate to use a data list in json in a web application.
I'm developing a REST Web Application, and im using Angular for front end, i should order, filter and print these data list also in xml ...
For you what scheme is better and why?
1) {
"datas": [
{ "first":"","second":""},
{ "first":"","second":""},
{ "first":"","second":""}
]
}
2) {
"datas": [{
"data": { "first":"","second":""},
"data": { "first":"","second":""},
"data": { "first":"","second":""}
}]
}
3) [
{ "first":"","second":""},
{ "first":"","second":""},
{ "first":"","second":""}
]
Thanks so much.
The first and third notations are quite similar because the third notation is included in your first.
So the question is "Should I return my datas as an array or should I return an object with a property that contain the array ?
It will depend on either you want to have more information alongside your datas or not.
For exemple, if your API might return an error, you will want to manage it from the front end.
In case of error, the JSON will looks like this :
{
"datas": null,
"error": "An error occured because of some reasons..."
}
At the opposite, if everything goes well and your API actually return the results, it will looks like this :
{
"datas": [
{ "first":"","second":""},
{ "first":"","second":""},
{ "first":"","second":""}
],
"error": null
}
Then your front end can use the error property to manage errors sent from the API.
var result = getDatas(); // Load datas from the API
if(result.error){
// Handle the error, display a message to the user, ...
} else {
doSomething(result.datas); // Use your datas
}
If you don't need to have extra properties like error then you can stick with the third schema.
The second notation is invalid. The datas array will contain only one object which will have one property named data. In this case data is a property that is defined multiple times so the object in the array will contain only the last occurence:
var result = {
"datas": [{
"data": { "first":"a","second":"b"},
"data": { "first":"c","second":"d"},
"data": { "first":"e","second":"f"}
}]
}
console.log("Content of result.datas[0].data : ")
console.log(result.datas[0].data)
Obviously the first option would be easy to use. Once you will access datas it'll give you an array. Any operation (filter, sort, print) on that array will be easy in comparison to anything else. Everywhere you just need to pass datas not datas.data.

Zapier Catch (Raw) Hook JSON parsing issue

I would like to configure sync between two different CRMs (Clevertap and Intercom) using Zapier and Webhooks. In general Clevertap sends the following JSON to webhook:
{
"targetId": 1548328164,
"profiles": [
{
"event_properties": {
"MSG-sms": true,
"MSG-push": true,
"businessRole": "EMPLOYEE",
"Mobile Number": "123123123123",
"Name": "Artem Hovtvianisa",
"Title": "Mr",
"Last Name": "Hovtvianisa",
"Gender": "M",
"Customer type": "Business Account Holder",
"MSG-email": true,
"First Name": "Artem",
"Last seen IP": "111.177.74.50",
"tz": "GMT+0200",
"International customer": "yes",
"isBusiness": true,
"Email": "xxxyyy#gmail.com",
"Identity": 15675
},
"objectId": "e32e4de3c1e84b2d9bab3707c92cd092",
"all_identities": [
"15675",
"xxxyyy#gmail.com"
],
"email": "xxxyyy#gmail.com",
"identity": "15675"
}
]
}
Zapier provides two types of catch webhook: regular and Raw.
Catch Raw Hook
When I use this type, JSON raw data will be handled OK and on the next step (Zapier JS code app) I am able to pass proper JSON data like in example above.
However when I use simple JS code to parse JSON object and get profiles[0] array value I get the following error "TypeError: Cannot read property '0' of undefined"
JS Code from Code step:
var result = JSON.parse(JSON.stringify(inputData));
console.log(result.profiles[0]);
return result;
Catch Hook
In case I use regular Catch Hook, hook parse data in some odd way, like this:
JSON.parse cannot recognize this structure.
Please advise how can I handle Webhook Zapier step in a proper way in order to get profiles[0] array item values?
Thanks in advance!
David here, from the Zapier Platform team. You're on the right track!
Catch Raw Hook is the way to go here. Your issue is that the data is coming in as a string and you're re-stringifying it before parsing it, which gets you back to where you came from. A simpler version:
JSON.stringify("asdf") // => "\"asdf\"", quotes in the string
JSON.parse("\"asdf\"") // => "asdf", the original string
"asdf".profiles // => undefined
undefined[0] // => error, cannot read property "0" of undefined
Instead, just parse it and you're good to go!
// all your variables are already in "inputData", so yours,
// also named inputData, must be referenced explicitly.
const result = JSON.parse(inputData.inputData);
return {result: result, firstProfile: result.profiles[0]};

extract from a JSON array (JMeter)

what is the best approach to capture from the following array?
i only need to capture the value of ANY 'beginDate', e.g: 2017-05-01T08:30:00 could be a valid one in below example
i need to make sure the 'beschikbaar' = TRUE for the date that i'm capturing
i tried using json path extractor with similar lines: $..[?(#.beschikbaar == 'true')].beginDate but i'm facing syntax errors that i cant fix due to my limited regex/json path knowledge
the example array is;
{
"data":
[
[
{
"beginDate":"2017-05-01T08:00:00",
"eindDate":null,
"beschikbaar":false
},
{
"beginDate":"2017-05-01T08:15:00",
"eindDate":null,
"beschikbaar":false
},
{
"beginDate":"2017-05-01T08:30:00",
"eindDate":"2017-05-01T10:30:00+02:00",
"beschikbaar":true
},
{
"beginDate":"2017-05-01T08:45:00",
"eindDate":"2017-05-01T10:45:00+02:00",
"beschikbaar":true
},
{
"beginDate":"2017-05-01T09:00:00",
"eindDate":"2017-05-01T11:00:00+02:00",
"beschikbaar":true
},
{
"beginDate":"2017-05-01T09:15:00",
"eindDate":"2017-05-01T11:15:00+02:00",
"beschikbaar":true
},
{
"beginDate":"2017-05-01T09:30:00",
"eindDate":"2017-05-01T11:30:00+02:00",
"beschikbaar":true
},
{
"beginDate":"2017-05-01T09:45:00",
"eindDate":"2017-05-01T11:45:00+02:00",
"beschikbaar":true
},
{
"beginDate":"2017-05-01T10:00:00",
"eindDate":"2017-05-01T12:00:00+02:00",
"beschikbaar":true
},
Don't use regular expressions for JSON data, JMeter provides JSON Extractor designed to work with JSON data via JSON Path Language so you should be able to get your "beginDate" with the query like:
$..[?(#.beschikbaar == true)].beginDate
Demo:
Check out JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios article for more detailed explanation and few more examples.
You can try this
(?s)\{.*?\"beginDate\":\"([^{]*?)\"[^{]+\"beschikbaar\":true.*?\}
(?s) is single-line modifier which makes . match the line break
You can test it at http://www.regexplanet.com/advanced/java/index.html
And set Template to $1$ means using the first group

How can I use AJAX with web methods to send JSON objects to a jQuery DataTable using asp.net?

So I have the jQuery datatable using AJAX to call for the JSON in this format.
$(document).ready(function () {
$('#test').DataTable({
ajax:{
url:"players.json",
dataSrc:""
},
columns: [
{data: "id"},
{ data: "player" },
{ data: "points" },
{ data: "steals" },
{ data: "blocks" },
{ data: "assists" },
{ data: "MPG" },
{ data: "shot %" },
{ data: "3 %" }
]
});
});
My aspx.cs file has a method to create the JSON file which works.
[System.Web.Services.WebMethod]
public static void loadTable()
{
NBAPlayerRepository players = new NBAPlayerRepository();
DataTable dt = players.GetAll();
var json = dt.ToJson();
System.IO.File.WriteAllText(#"C:\Users\wheres\Downloads\nbaStats\nbaStats\nbaStats\players.json", json);
}
And the JSON looks like this:
[{"id" : "67926aa7-46b7-4418-96db-fc7e5216aac4","playername" : "Wilson Heres","points" : "34534","steals" : "34","blocks" : "34","assists" : "343","mpg" : "343","shootingpercentage" : "33.3429985046387","threepointpercentage" : "33.3429985046387"}
,{"id" : "6dc42e0b-8750-463d-a9ef-5a025a27154b","playername" : "Wilson Heres","points" : "34534","steals" : "34","blocks" : "34","assists" : "343","mpg" : "343","shootingpercentage" : "33.3429985046387","threepointpercentage" : "343.334014892578"}
,{"id" : "f727130c-5b94-4730-a653-cfb603c73b8a","playername" : "Wilson Heres","points" : "34534","steals" : "34","blocks" : "34","assists" : "343","mpg" : "343","shootingpercentage" : "33.3429985046387","threepointpercentage" : "343.334014892578"}
]
But now I am getting this error "jquery.dataTables.min.js:48 Uncaught TypeError: Cannot read property 'length' of undefined"
Edit: This all works now. Just had to add dataSrc:""
Cleaned-up version of your DataTables initialization
$(document).ready(function () {
$('#test').DataTable({
ajax: {
url: "players.aspx/loadTable"
},
columns: [
{ data: "id" },
{ data: "player" },
{ data: "points" },
{ data: "steals" },
{ data: "blocks" },
{ data: "assists" },
{ data: "MPG" },
{ data: "Shot %" },
{ data: "3 %" },
]
});
});
This may seem like a lot of stuff was removed from your code, so let me make some explanations/assumptions about what was changed.
Assumptions
First, an assumption: DataTables will always try to use a GET request, not a POST when first getting the data from the table, so make sure that your data processing code expects that.
I also am assuming that you have no strong desire to have your Ajax separate from your initialization and that was just how you decided to do it as a first attempt. If that is the case, let me know and I'll update the code to match that.
Explanation
Your formatting is incorrect in some areas and against DataTables standard in others, so this version should do most of what you were trying to do in a much simpler form. A lot of your Ajax options are unnecessary because they are already the default (JSON for the data type, for example), which is why they have been removed.
One nice thing about DataTables is that you can have the Ajax options in the initialization, which is what I have done here. You do lose the success and failure callbacks but I think that for debugging purposes they aren't really necessary and having any extra code increases the amount of stuff to debug (I don't even use those callbacks in most of my final code).
Most of the other changes were mainly incorrect nomenclature (e.g. data instead of title in the column definitions.
Disclaimer
While I would recommend these changes in general just to improve your code, I would make sure to take a look at the format of the JSON that is being sent to and from the server. If you don't know how to do that I'd recommend downloading Fiddler to 'listen in' on the JSON data being sent.
If your JSON is wrong, no amount of changes to the page are going to make the table appear.
Finally, make sure you have no JS errors on the page. Use your browser's developer console (F12) to check that.
If you do find any JS errors, post them in your question. I'd also recommend posting the JSON data being sent to the question as well so that we can ensure the format is correct.

Datatables process json response multiple objects

Im using Datatables Jquery plugin to do a bit of work, but i havent been able to understand how to process the Json response im getting from the server .
I get this response:
"aaData":[
[
{
"idTreatment":23,
"treatment":"Hospitalización",
"directions":"Directions",
"active":"1"
},
{
"idCIE10":1,
"cieCode":"A00",
"diagnostic":"COLERA",
"description":null,
"lastUsed":1386649580000,
"active":1
}
],
[
{
"idTreatment":27,
"treatment":"Antibiótico",
"directions":null,
"active":"1"
},
{
"idCIE10":1,
"cieCode":"A00",
"diagnostic":"COLERA",
"description":null,
"lastUsed":1386649580000,
"active":1
}
],
[
{
"idTreatment":33,
"treatment":"Hidratación Oral",
"directions":"Directions",
"active":"1"
},
{
"idCIE10":1,
"cieCode":"A00",
"diagnostic":"COLERA",
"description":null,
"lastUsed":1386649580000,
"active":1
}
]
]
}
I have been trying to use mData or mRender, but im not familiarized with datatables yet, so i would be really grateful if someone could thell me how can in print this in a table on html
thanks in advance
this javascript fucntion is the one im using to initialize the table, im trying to use aoColumns based on examples but im not sure if this is the best option; also based on example im using mData, to show just 2 columns for test, as you can see im trying to acces the "array" or the json response using the objects field names, but when the page is rendering theres a warning saying that it cant find the field.
function initializeTreatmentsTable(id){
treatmentsTable = $('#treatmentsTable').dataTable( {
"bSort":false,
"sAjaxSource":"./diagnosticTreatment",
"fnServerParams": function ( aoData ) { aoData.push( {name:"diagnosticId",value:id} ); },
"aoColumns": [
{ "mData": "idTreatment" },
{ "mData": "treatment" }
]
});
Well after looking several times over the API in the official website datatables.net i came to the conclusion that i had to test out.
If my problem isn't clear i will explain it again.
i have a table, and that table's data is obtained by a ajax request and a json response, the json response is a array of object but the objects themselves are composed of at least 2 objects ( {[[object1][object2]],[[object1][object2]]} ) so accessing them with mData was impossible at least for me, and the solution was so simple that i wonder i never tried, i just had to use mRender and a function.
function initializeTreatmentsTable(id){
treatmentsTable = $('#treatmentsTable').dataTable( {
"bSort":false,
"sAjaxSource":"./diagnosticTreatment",
"fnServerParams": function ( aoData ) { aoData.push( {name:"diagnosticId",value:id} ); },
"aoColumns": [
{ "bVisible": false,
"mRender": function(data,type,full){
return data.idTreatment;}
},
{ "mRender": function(data,type,full){
return full[0].treatment;
} }
]
});
The function above its the one im using to initialise the table for the first time, i got the problem that i just wanted 2 field from the first object and none from the second one, but i dont now how to do it, so i used the "full" parameter that contains the whole json response, and accesed it.
This might not be the best answer but it did the work for me, so i hope that if someone know how to improved it , feel free to do it, or so just comment , and i also hope this can help someone else.