0

Apologise in advance for the previous post. @xavdid did a great job helping me out. Due to my lack of expertise and knowledge in this field I failed to express accurately what I needed. I believe I have now enough information to express what I want to achieve. So I will do my best to express it here.

Here is my input information, Keys and Values. Each position of the keys corresponds to the position of the values.

I believed in order to solve this problem, I would need to know when a book starts and when it doesn't start. I was wrong.

All I need is to match the predefined keys with their values and group them together.

By predefined keys I mean returning only these 7 keys :"Project Details,Project Title,Addons,Upgrade,Word Count,Ebook Type,Upload your file here" with their values (ignore the other keys)

Example of 3 books:

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

Input Data Values: Book Description 3,Book Title 3,Book Cover Design - $59.00,No Package,Standard 10K - $270.00,Standard Ebook,Start~~//www.shappify-cdn.com/images/282516/127828454/001_Ebook Standard 325x325.png~~start,start1526659928051,1,https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/778dfc3dbdf278441776e9f5dd763910.png,start1526659928051,1,start1526659872230,Start~~//www.shappify-cdn.com/images/282516/127828455/001_Ebook Technical 325x325 (1).png~~start,Technical Ebook,Technical 15K - $450.00,No Package,No Addons,Book Title 2,Book Description 2,Book 1 Description,Book 1 Title,No Addons,Essential Package - $79.00,Standard 20K - $540.00,Standard Ebook,Start~~//www.shappify-cdn.com/images/282516/127828458/001_Ebook Standard 20k 325x325.png~~start,start1526659838425,1,https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/c09635c2e003fd8779a19651e36f4315.png,start1526659838425

Output desired:

[{'Ebook Type': 'Standard Ebook'},{'Ebook Type':'Technical Ebook'},{'Ebook Type':'Standard Ebook'}], [{'Word Count': 'Standard 10K - $270.00'}, {'Word Count': 'Technical 15K - $450.00'},{'Word Count': 'Standard 20K - $540.00'}] [{Upgrade: 'No Package'},{Upgrade: 'No Package'},{Upgrade: 'Essential Package - $79.00'}] [{Project Title: 'Book Title 3'}, {Project Title: 'Book Title 2'}, {Project Title: 'Book Title 1'}] [{'Project Details': 'Book Description 3'},{'Project Details': 'Book Description 2'},{'Project Details':'Book 1 Description'}], [{'Addons: 'Book Cover Design - $59.00',{Addons:'No Addons'},{Addons:'No Addons'}], [{'Upload your file here':'https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/778dfc3dbdf278441776e9f5dd763910.png'},{'Upload your file here':https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/c09635c2e003fd8779a19651e36f4315.png'}]

Thank you very much

Fabian C
  • 5
  • 3
  • presumably you want the output to be arrays of strings? as written, it's not valid json/js. Also, given that there's nothing zapier specific about this, you might have more luck tagging the question with javascript or similar – xavdid May 18 '18 at 18:16
  • Ye I would like the output to be in a way that Zapier could understand it, so it can be displayed in the GUI. – Fabian C May 18 '18 at 18:43
  • The reason I tagged in Zapier, is that it is for the platform and I am not really sure how the output has to look like. All I know is that I want it as separated objects. Let's take Project Title for instance. I believe in Zapier it would show Project Title: 1: Book Title 1 2: Book Title 2 3: Book Title. In a way that if I paste it in Google Sheets with the multiple row option, it would create 3 rows, and not paste in one row just as one string. If that makes sense. Sorry for my non technical explanation – Fabian C May 18 '18 at 18:59
  • don't worry about the UI as much, worry about the structure of the data. for downstream actions, do you want to do 1 action with all 3 books (1 row with 3 books) or 3 actions for 3 books (ie, they each get their own row)? It's the same question even if you're connecting the output to a different app – xavdid May 18 '18 at 19:06
  • 3 actions for 3 books. They get each their own row – Fabian C May 18 '18 at 19:12
  • Got it. So the final structure you want to return is `[{title: 'book1'}, {title: 'book2'}, {title: 'book3'}]`. to do this, you'll need to separate the books from input. It seems like the keys aren't separated by book. if you know each key is order relative to itself (title 2 is somewhere after title 1, even if there's other keys between) then you could group as described in the second structure and split that way. – xavdid May 18 '18 at 19:41
  • again though, if you control the way the ebook info is sent to zapier, then there's a less fragile/easier way to do all this – xavdid May 18 '18 at 19:42
  • Exactly that would be the final structure. I don't really control the way the ebook info is sent to Zapier, it is a plugin that sends that info in that way. What is for sure is that all keys and values from book 2 are after book 1 and before book 3. So it will always follow a structure of [{title: 'book1'}, {title: 'book2'}, {title: 'book3'}] [{description: 'desc1'}, {description: 'desc2'}, {description: 'desc3''}] and then when pasting it, say in an app for Sheets if I paste in Column Title: {Title} and Description: {Description}, they will always match. – Fabian C May 18 '18 at 20:06
  • Just updated the question, that is how it would have to look right? – Fabian C May 19 '18 at 00:11
  • sorry, i was unclear. for the sake of brevity. what you want is `[{title: 'book1', description: 'desk1', ...}, {title: 'book2', description: 'desc2', ...}, {title: 'book3', description: 'desc3', ...}]`. Anything you return needs to be valid json. it sounds like I have enough to give it a shot, but i'm still not following what you're pasting. zapier is hopefully doing that work for you – xavdid May 19 '18 at 17:29
  • Let's take for example a common line in Zapier: Line Items Quantity. If there are 4 items purchased and x1 of each, values will be 1,1,1,1. If I were to use this value in Google Sheets, it would create 4 rows with a 1 on each row. I need exactly that format for each key. Instead of Line Items Quantity for instance. Project Description: Desc1,Desc2,Desc3 therefore creating 1 row with Desc 1 another row with Desc2 another tow with Desc3 – Fabian C May 19 '18 at 17:36

1 Answers1

0

Ok! So I think I have this how we want it. The big issues are the assumptions we're making about the structure of the data, namely:

  • there are never commas in values (A book titled "Murder, She Wrote" would bust this because of the comma splitting)
  • the keys can be in any order relative to each other (might do any number of titles before descriptions) but title 3 will aways come (somewhere) before title 2

Given that, we need to split up our inputs, separate into the 7 keys we want, and then build books out of those arrays. That is the following:

const keys = inputData.keys.split(',')
const values = inputData.values.split(',')
const keysWeWant = "Project Details,Project Title,Addons,Upgrade,Word Count,Ebook Type,Upload your file here".split(',')

let orderedValues = {}
// fill arrays so we can blindly push
keysWeWant.forEach(k => orderedValues[k] = [])

keys.forEach((key, index) => {
  if (keysWeWant.includes(key)) {
    orderedValues[key].push(values[index])
  }
})

console.log(orderedValues)

// have to cut up books now
// don't know how many we have, hopefully our values have the right number of items
// .map(Object) is because of this: https://stackoverflow.com/questions/35578478/array-prototype-fill-with-object-passes-reference-and-not-new-instance
let result = Array(orderedValues['Project Title'].length)
  .fill()
  .map(Object)

for (const keyWeWant in orderedValues) {
  orderedValues[keyWeWant].forEach((val, index) => {
    result[index][keyWeWant] = val
  })
}

// put book 1 first, this can be removed
result.reverse()
return result

you can see it in action here. Worth noting that in the sample data, there were only two "upload your file here", so the last book (book 1) is missing that key.

Also, when you test this in the zap editor, it'll look like only 1 item is returned. That's just for the test. It'll work normally when it's on an running for real (everything after the code step will run once for each item in the returned array).

xavdid
  • 5,092
  • 3
  • 20
  • 32
  • You nailed it @xavdid I just tested and it worked flawlessly. Also appreciate your effort for understanding me, I know it might have been challenging. I will mark this as solved. Thank you. – Fabian C May 19 '18 at 18:59
  • Ok, just tested it with a regular Project Description with commas on it. Indeed there is an issue with that, there is no work around in regards to the commas right? – Fabian C May 19 '18 at 19:03
  • not that i can think of. this data looks like csv, which should have quotes around things with commas, eg `title 1,"murder, she wrote",title 3`. the code could be amended to look for that. ideally, it would be passed as an json string, like so: `["title 1", "murder, she wrote", "title 3"]`. that's very clear about when each item begins and ends. but yeah, if the data doesn't change i'm not sure there's much else we can do :( – xavdid May 19 '18 at 23:02
  • I am not sure how to check how the input data comes. Zapier UI though, shows it exactly as you describe https://imgur.com/a/BBjAvXc – Fabian C May 20 '18 at 02:56
  • Ah, then you might be able to use a `JSON.parse`, which will keep the books separate. Might need to wrap the outer layer in `[]` so it's valid, but that'll be a good change – xavdid May 21 '18 at 03:06
  • Thank you @xavdid, myself I am not a coder so I don't think I will be able to do it. I can pass this information to a coder in Upwork, do you think showing him the code you wrote along with the last message should be enough description or the best way to go to sort it out? Cheers – Fabian C May 21 '18 at 10:57
  • I think that will work. This is pretty straightforward javascript and not zapier specific. They'll be able to handle or you can treat it as a learning opportunity! – xavdid May 21 '18 at 16:26
  • Seems like in Zap Editor we are able to see the objects separated, but when we want to use this data, looks just like a csv(using console.log(input data) shows just comma separated values) Can it be that the Zap Editor is turning the array of objects into just csv format? – Fabian C May 21 '18 at 20:09
  • ah, so it's coming through as a line item. Luckily, we've got a lot of resources about that. check out [this](https://zapier.com/blog/formatter-line-item-automation/) and [this](https://zapier.com/help/how-use-line-items-formatter/). Writing into support (`contact@zapier.com`) may also prove useful, as they've got a lot of experience helping make workflows happen (just not writing code) – xavdid May 22 '18 at 00:12
  • Went there in the first place, but they redirected me here, precisely because of the splitting problem. Using split in Zap edit has exactly the same effect like what we experienced here. I am really stuck and about to give up haha – Fabian C May 22 '18 at 02:45
  • I think I might have found a solution @xavdid. Thanks to your team for the suggestion. Using the webhook feature I am able to get the information displayed in a different way. See: http://pasted.co/a2396f6b. If you see those lines starting with properties, we can appreciate that the values come in this format: {u'name': u'Ebook Type', u'value': u'Standard Ebook'}, {u'name': u'Word Count', u'value': u'Standard 40K - $1080.00'}. And we can certainly differentiate each book, as each has their own line of properties. I think now we can work with this? – Fabian C May 23 '18 at 14:01
  • great, glad to hear that worked out! if you return an array from a code step, you'll be able to do everything after the code step once per item. Should work nicely. – xavdid May 23 '18 at 18:09
  • Appreciate it! @xavdid I managed to use only formatter with this new code. I used split text option, for the word count for instance I used " u'name': u'Word Count', u'value': u' " as the separator, so with this I had on the first sentence the value I wanted. Then split again using '} (where it ends) and selected pick First. Like this I had all the values I wanted. Cheers again – Fabian C May 29 '18 at 19:26