skip to Main Content

I will receive input values with two combinations either numeric or string value. If input is number you need to map it to one output field and if the input is string map to other field.

Eg, Input:

[{
"Input": "ABC123"
 },
 {
  "Input": "12345"
  }]

Output:

[{
 "String": "ABC123"
 },
 {
 "Number": "12345"
 }]

2

Answers


  1. You can use the json filter to determine the type since it will wrap a string in quotes, but not numbers. Check for the ” character and you’ll know if it’s a number. Will also cover your edge case.

    {% assign var1 = "1" %}
    {% capture testValue %}{{ var1 | json }}{% endcapture %}
    {% if testValue contains '"' %}string{% else %}number{% endif %}
    
    Login or Signup to reply.
  2. Dotliquid does not have this functionality at this moment. It is open source project and you can download source code and change/Add new method in StandardFilters.cs. You can then use it for fulfil your requirement. you can add as many operation you like for your custom requirement.

    e.g
    Add below method in StandardFilters.cs

    public static bool IsNumeric(object o)
    {
        return double.TryParse(Convert.ToString(o), out double result);            
    }
    

    you can use this method in your liquid template like,

    {% assign string = '' %}
    {% assign number = '' %}
    {% assign isNumber = model.Input | IsNumeric %}
    {% if isNumber == true %}
      {% assign number = model.Input %}
    {% else %}
      {% assign string = model.Input %}
    {% endif %}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search