cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Convert list of string to list of numbers

shefali
Deputy Chef I
Deputy Chef I

Is there a formula that can be used to convert list of String variables to a list of integers ? 

 

I/P is ["1","100","50", "4"] 
O/P [1,100,50,4]

1 ACCEPTED SOLUTION

Hi @shefali,
If you observe my screenshots:

1๏ธโƒฃ In the 2nd and 3rd images,
I used Variable_name: List and Datatype: String. When applying the pluck("v") formula, it automatically converts to List_of_Numbers, so there's no need to explicitly set the data type as List.

2๏ธโƒฃ I have a small curiosity: Why do we need to create two variables with Integer or Number data types to parse a number list?
Answer: There's no need to use two variables. As I suggested, when we apply the pluck("v") formula, it automatically takes care of converting to List_of_Numbers.

3๏ธโƒฃ However, when using list functions, it still treats the variable as a list. Shouldn't it actually be recognized as a number instead?
Could you please share a screenshot? I didnโ€™t fully understand your point here.

Looking forward to your insights! ๐Ÿ˜Š

Thanks and Regards,
Shivakumar K A

View solution in original post

20 REPLIES 20

Hi Prudvi,

Here is my sample JSON that needs to be parsed. I am first parsing the given JSON. Then, I use TestValueSampleList.pluck("v") to extract a list of strings, which I then convert into a list of integers using native functions and formulas.

 

{
    "TestValueSample": [
        {
            "k1": "t1",
            "v": "9"
        },
        {
            "k1": "t2",
            "v": "11"
        },
        {
            "k1": "t3",
            "v": "21"
        },
        {
            "k1": "t4",
            "v": "7"
        }
    ]
}

 

 

Hi @shefali ,

Here, I am adding a screenshot after parsing the provided JSON.

- 2nd Image: The input hasnโ€™t been explicitly converted to an integer, yet the output is a list of integers.
- 4th Image: If this doesn't work on your end, you can use another variable of type "Integer" and pass the output from the `pluck("v")` formula.Parse_json_1.PNGparse_json_2.PNGparse_json_3.PNGparse_json_4.PNGparse_json_5.PNGparse_json_6.PNG

Thanks and Regards,
Shivakumar

 

Thanks, Shiva! Your solution worked perfectly for meโ€”I really appreciate it.

I do have a small curiosity: Why do we need to create two variables of integer or number data type to parse a number list? For example, after the JSON parse step, I attempted to create a list of numbers, and the output appeared as expectedโ€”a list of numbers. However, when using list functions, it still treats the variable as a list. Shouldn't it actually be recognized as a number instead?

Is this the expected behavior of the function, or could it be a bug? It feels a bit misleading and requires a lot of trial and error to get right

Hi @shefali,
If you observe my screenshots:

1๏ธโƒฃ In the 2nd and 3rd images,
I used Variable_name: List and Datatype: String. When applying the pluck("v") formula, it automatically converts to List_of_Numbers, so there's no need to explicitly set the data type as List.

2๏ธโƒฃ I have a small curiosity: Why do we need to create two variables with Integer or Number data types to parse a number list?
Answer: There's no need to use two variables. As I suggested, when we apply the pluck("v") formula, it automatically takes care of converting to List_of_Numbers.

3๏ธโƒฃ However, when using list functions, it still treats the variable as a list. Shouldn't it actually be recognized as a number instead?
Could you please share a screenshot? I didnโ€™t fully understand your point here.

Looking forward to your insights! ๐Ÿ˜Š

Thanks and Regards,
Shivakumar K A

Hi Shiva,

Sure. My previous comment was about whether it's possible to apply pluck and min on the same list. For example: TestSample.pluck('v').min().  Currently, this operation works based on ASCII character calculation when applied to strings. To get the maximum integer value, we first need to create a list of strings/integers and then use a variable of type integer to extract the maximum value.

Thanks for your help anyway. Appreciate it

That was the reason for my question. We can mark this as resolved if thereโ€™s no way to apply pluck and max on integers in the same step.