Handlebars to remove new line and return text in between characters

Hi Good Afternoon,

I am currently running into an issue while trying to remove the json formatting to remove the new line symbol and extract data between text. I am successfully able to extract the information using this handlebar "{{split (split note "IndustryType:" 1) "IfOTHERdescribe:" 0}}" and I am successfully able to remove the new line "\n" using this handlebar "{{replace (jsonEncode [note])"\n" ", "}}" but when I attempt to combine them such as "{{{replace (split (split note "IndustryType:" 1) "IfOTHERdescribe:" 0)(jsonEncode [note])"\n" ", "}}}" or this "{{replace (jsonEncode [note])"\n" ", "(split (split note "IndustryType:" 1) "IfOTHERdescribe:" 0)}}" it either does one or the other and not both. The text and formatting of the text being

"note": "CompanyName: Placeholder\nPosition: None\ntext-ZIED: None\nFederalTaxID: None\nResaleTaxCertificateNo: None\nResaleTaxCertificate: None\nSecondaryPhone: None\nFax: None\nLocksmithLicenseNumber: None\nIndustryType: Contractor\nIfOTHERdescribe: None\nAreYouDahuaDealer: False\nSalesContactDahuaDealer: False\nProductCategories: None\nYearsinBusiness: 0\nNumberofLocksmiths: 0\nDoyouhaveastorefront: None\nNoOfLocations: None\nAnnualSales: None\nNumberofVehicles: None\nPrimarySupplier: None\nSecondarySupplier: None\nHowdidyoufindus: None\nAgreeToTerms: True",

1

Comments

5 comments
Date Votes
  • Shadner Joseph will this work for you?

    {{split (replace (jsonEncode note) "\n" ",") "," 0}}
    {{split (split (replace (jsonEncode note) "\n" ",") "," 0) ": " 0}}
    {{split (split (replace (jsonEncode note) "\n" ",") "," 0) ": " 1}}

    {{split (replace (jsonEncode note) "\n" ",") "," 1}}
    {{split (split (replace (jsonEncode note) "\n" ",") "," 1) ": " 0}}
    {{split (split (replace (jsonEncode note) "\n" ",") "," 1) ": " 1}}

    {{split (replace (jsonEncode note) "\n" ",") "," 2}}
    {{split (split (replace (jsonEncode note) "\n" ",") "," 2) ": " 0}}
    {{split (split (replace (jsonEncode note) "\n" ",") "," 2) ": " 1}}

     

    Additionally, if the fields and value pairs aren't always in the same position, you may need a script to convert the string to JSON. Here is a sample transform below:

    /*
    * transformFunction stub:
    *
    * The name of the function can be changed to anything you like.
    *
    * The function will be passed one 'options' argument that has the following fields:
    *   'record' - object {} or array [] depending on the data source.
    *   'settings' - all custom settings in scope for the transform currently running.
    * The function needs to return the transformed record.
    * Throwing an exception will return an error for the record.
    */
    function transform (options) {
      
      options.record.noteJSON = {};
      if (options.record.note.split('\n').length > 0) {
        for (let [index, n] of options.record.note.split('\n').entries()) {
          if (n.split(': ').length > 0) {
            options.record.noteJSON[n.split(': ')[0]] = n.split(': ')[1];
          }
        }
      }
      
      return options.record;
    }

    2
  • Thank You!

    The solution below was exactly what I needed! 

    {{split (split (replace (jsonEncode note) "\n" ",") "," 0) ": " 1}}
    0
  • Tyler Lamparter,

    How come the following expression with the following input returns nothing when the next input returns '123456' correctly?

    {{split (split (replace (jsonEncode note) "\ncustporef" ",") "," 0) ": " 1}}

    Returns Nothing:

       "note": "notes\n\nPlease remember:\n- PO# 123456 on the order\n- Ship Blind\n- Overnight AM Please\n - \ncustporef: 123456",

    Returns 123456:

       "note": "notes\n\nPlease remember:\n- PO# 123456 on the order\n- Ship Blind\n- Overnight AM Please\n - \ncust_po_ref: 123456",
    0
  • Jared Fraley it's easier to visualize why if you break out each piece of the handlebar expression. See below results:

     

    When you replace \ncustporef with a comma, you are then splitting everything based on commas, but then choosing the first index of the split. In the 3rd output, you see you no longer have access to the 123456 number since you chose index 0 after splitting on the comma. If you change it to use index 1, you'll get what you need.

    {{jsonEncode note}}

    {{replace (jsonEncode note) "\ncustporef" ","}}

    {{split (replace (jsonEncode note) "\ncustporef" ",") "," 1}}

    {{split (split (replace (jsonEncode note) "\ncustporef" ",") "," 1) ": " 1}}
    1
  • Tyler Lamparter,

    Excellent, thank you for the visualization and explanation!

    0

Please sign in to leave a comment.

 

Didn't find what you were looking for?

New post