Handlebars to remove new line and return text in between characters

Comments

5 comments

  • Tyler Lamparter Principal Product Manager
    Awesome Follow-up
    Engaged
    Top Contributor
    Answer Pro
    Celigo University Level 4: Legendary

    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
  • Tyler Lamparter Principal Product Manager
    Awesome Follow-up
    Engaged
    Top Contributor
    Answer Pro
    Celigo University Level 4: Legendary

    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
  • Shadner Joseph

    Thank You!

    The solution below was exactly what I needed! 

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

    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

    Tyler Lamparter,

    Excellent, thank you for the visualization and explanation!

    0

Please sign in to leave a comment.