Zapier - Input data sorting

廉价感情. 提交于 2019-12-24 20:57:43

问题


Here is what happens:

I am using a very cool plugin in my Shopify store that allows you to customise your product following a sequence of steps.

To illustrate

Problem is that all of this information is not sorted for Zapier. Essentially it displays as this:

The issue

They come as " Line Items Properties Values " and "Line Items Properties Names" and when pasted they come as an array.

What I would need to do is to match those Names with their Values. And if possible be able then to select in the GUI of Zapier.

So instead of having these fields and values

"Line Properties Names" -> "Project Title","Project Description","Ebook Type"....

"Line Items Properties Values" -> "Sherlock Holmes","A story in London..", "Standard Book"...

Having these fields and values:

"Project Title" -> "Sherlock Holmes"
"Project Description" -> "A story in London.."
"Ebook Type" -> "Standard Ebook"

Would it be possible?

Thank you for your time

UPDATE

For clarification purposes

So, in this order there are 3 different products. Separated by []. Values within the product can vary, for instance if a customer decides to not fill the field of " Project Details ", then Project Details Keys and Value wont display. Resulting essentially in products with different amount of keys and values.

Here is an example: (As you can see the first product has a different set of values as the second product)

Input data

Input data Values: ["1","ebook1524837342394","Ebook~~//www.shappify-cdn.com/images/282516/127828455/001_Ebook Technical 325x325 (1).png~~ebook","Technical Ebook","Technical 15K - $450.00","All Inclusive Package - $149.00","Cookbook Instant Pot"],["ebook1524837342394"],["Detective Story based in London......","Sherlock Holmes","No Addons","No Package","10000 Words - $270.00","Fiction Book","Ebook~~//www.shappify-cdn.com/images/282516/127828453/001_Ebook Standard 325x325.png~~ebook","ebook1524837304725","1","https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/7ddee14d6e5d6c5e4396981d1702c375.jpg"]

 Input data Keys: ["_master_builder","_builder_id","_builder_info","Ebook Type","Word Count","Upgrade","Project Title"],["_builder_id"],["Project Details","Project Title","Addons","Upgrade","Word Count","Ebook Type","_builder_info","_builder_id","_master_builder","Upload your file here"]

What I want to do

I want to match the key with their value and be able to select them on the Zapier GUI.

Current output with the code proposed

Output 1stpart

Output 2ndpart

Output 3rdpart

Output 4thpart

Expected Output

[{"_master_builder":"1","_builder_id":"ebook1524837342394","_builder_info":"Ebook~~//www.shappify-cdn.com/images/282516/127828455/001_Ebook Technical 325x325 (1).png~~ebook"...}]

Appreciate your help guys


回答1:


Ah! So this is a little trickier than it looked at first, but it's not bad. There are two gotchas:

  • Your data is coming in as a string
  • that string is made of multiple array objects, but isn't itself a valid array object

So once we parse it out correctly, it's not too bad.

// just used for testing outside zapier
// these are comma separated strings
const inputData = {
  keys: '_master_builder,_builder_id,_builder_info,Ebook Type,Word Count,Upgrade,Project Title,_builder_id,Project Details,Project Title,Addons,Upgrade,Word Count,Ebook Type,_builder_info,_builder_id,_master_builder,Upload your file here',
  values: '1,ebook1524837342394,Ebook~~//www.shappify-cdn.com/images/282516/127828455/001_Ebook Technical 325x325 (1).png~~ebook,Technical Ebook,Technical 15K - $450.00,All Inclusive Package - $149.00,Cookbook Instant Pot,ebook1524837342394,Detective Story based in London......,Sherlock Holmes,No Addons,No Package,10000 Words - $270.00,Fiction Book,Ebook~~//www.shappify-cdn.com/images/282516/127828453/001_Ebook Standard 325x325.png~~ebook,ebook1524837304725,1,https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/7ddee14d6e5d6c5e4396981d1702c375.jpg'
}

// arrays must be the same length
const zipArrays = (a, b) => {
  let res = {}
  a.forEach((val, i) => {
    res[val] = b[i]
  })
  return res
}

// have to convert strings to actual arrays
// this will blow up if any of the data has commas in it
const keys = inputData.keys.split(',')
const vals = inputData.values.split(',')

// now we have real arrays

const result = {}

// copy keys onto the result, overwriting old ones
Object.assign(result, zipArrays(keys, vals))

console.log(result)
/*
  { _master_builder: '1',
    _builder_id: 'ebook1524837304725',
    _builder_info: 'Ebook~~//www.shappify-cdn.com/images/282516/127828453/001_Ebook Standard 325x325.png~~ebook',
    'Ebook Type': 'Fiction Book',
    'Word Count': '10000 Words - $270.00',
    Upgrade: 'No Package',
    'Project Title': 'Sherlock Holmes',
    'Project Details': 'Detective Story based in London......',
    Addons: 'No Addons',
    'Upload your file here': 'https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/7ddee14d6e5d6c5e4396981d1702c375.jpg' }
*/

// return result

As is, there's a lot of repeated keys keys so the output is smaller than the input. You can also tweak this code to match your input better if you want to group the output differently.

Hope that helps!




回答2:


This should be possible with a for loop. Try this.

In your example, define the first box as values and the second as keys. Remove the var inputData... lines before running the code step.

var inputData = {"keys": ["First", "Second", "Third"],
                "values": ["One", "Two", "Three"]
                };
//Remove the lines above before pasting in the Code step. 
//You will need to configure it in the Zap.

var product = {};
for (var i = 0; i < inputData.keys.length; i++) {
  var commonkey = inputData.keys[i];
  product[commonkey] = inputData.values[i];
}
console.log(JSON.stringify([product]));
   
// already available in the zapier scope
output = [product]


来源:https://stackoverflow.com/questions/50102390/zapier-input-data-sorting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!