Skip to main content
Best answer

Convert text


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?

Best answer by TimSBest answer by TimS

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!

View original
Did this topic help you find an answer to your question?
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

ikbelkirasan
Forum|alt.badge.img+12
  • Zapier Expert
  • 555 replies
  • July 29, 2020

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 }];

  • Author
  • Beginner
  • 2 replies
  • July 29, 2020

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


TimS
Forum|alt.badge.img+3
  • Zapier Staff
  • 21 replies
  • Answer
  • September 1, 2020

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!


nicksimard
Forum|alt.badge.img+11
  • Zapier Staff
  • 2115 replies
  • September 4, 2020

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


  • Author
  • Beginner
  • 2 replies
  • September 4, 2020

Hey thanks alot, cant wait to try it