Best answer

Convert text

  • 25 July 2020
  • 5 replies
  • 474 views

Userlevel 2

I would like to convert a text like this:

||key = xxxxxxx||pair=btcusdt||exchange=binance||type=entry||stopLossPercentage={{plot_0}} ||exchangeAccountType=futures||leverage=2||side=long||positionSizePercentage=20||orderType=market||signalId=btclongfutures||

 

to something like this:

 

{"key":"YOU_KEY","pair":"STRATBTC","exchange":"binance","type":"entry"}

 

So would nee to add { } for beginning and end, and change = for : , remove || and put “ between each existing words

 

Is this possible?

icon

Best answer by TimS 1 September 2020, 19:41

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

5 replies

Userlevel 7
Badge +12

Hi @TrendSurfers - This can be done with a JavaScript code step. You can use the code snippet below to parse that input into a readable object. The input text should be mapped to an input field called text.

const { text } = inputData;

const result = text
.split("||")
.filter((item) => !!item)
.map((item) => {
return item.split("=").map((item) => item.trim());
})
.reduce((target, current) => {
const [key, value] = current;
target[key] = value;
return target;
}, {});

output = [{ result }];
Userlevel 2

Thanks, this is out of my league hehe.. i kinda understand the code a bit, but wouldnt know how to make use of it.. 

Thanks for taking the time tho. Ill see what i i can do with it

Userlevel 7
Badge +3

Hi @TrendSurfers ! 

Just in case you’re still working on this, I wanted to show you what this code would look like in practise. @ikbelkirasan ‘s code is spot on - you can simply copy and paste it into a “Run Javascript” Step that is part of the “Code by Zapier” App integration. Here’s what it looks like:

As you can see, we’ve set one Input Data field as “text” to match the code using “text” from the inputData Object.

 

When we test this Step, it shows us this result:

Is this what you were looking for, or were you wanting a text JSON representation? If you want text instead of the individual values, change the last line of code to this:

output = [{ result: JSON.stringify(result) }];

When we test the code with that modification, we get: 

I hope this helps clarify how you can use this in your Zaps!

Userlevel 7
Badge +11

Thanks for the assist @TimS :) Super helpful to see the additional breakdown, and with screenshots!

Userlevel 2

Hey thanks alot, cant wait to try it