Hey all, my last step for my script makes me stuck. I recieve the following json:
“status”: “success”,
“activeActivations”: [
{
“activationId”: “1624962258”,
“serviceCode”: “ot”,
“phoneNumber”: “37065199434”,
“activationCost”: “15.00”,
“activationStatus”: “2”,
“smsCode”: [
“Your Orderd verification number is: 3522”
],
“smsText”: [
“Your Orderd verification number is: 3522”
I need to fetch the number from the line: “Your Orderd verification number is: 3522”
So in this case only the 3522
I just red a few other threads but as I´m a bloody beginner i guess i will need further help.
I know I will get super quick responses from you and want to thank you in advance.
ppr
(Peter Preuss)
July 30, 2023, 8:12pm
2
We fixed the snippet ot make it as a valid JSON and deserilized it - out: myJObject
Samples:
1st: access
2nd: Regex for the number extraction
HEy thanks for ur replay.
Deserilization is already done. I just wanted to show the value i want to fetch.
I really have no experience with regex. How to do it and how to implant?
mkankatala
(Mahesh Kankatala)
July 30, 2023, 8:18pm
4
Hi @Hugen_Dugen
Use the below regular expression to extract the required data from the input.
(?<=Your Orderd verification number is\:\s+)\d+
Hope it helps!!
HEy ty for replay. i guess i have to use an assign function for this right?
1 Like
ppr
(Peter Preuss)
July 30, 2023, 8:26pm
6
the shown samples were done within the immediate panel:
Assign activity:
myValue | DataType String = myJObject(“activeActivations”)(0)(“smsCode”)(0).Value(Of String)
myCode | DataType String =
System.Text.RegularExpressions.Regex.Match(myValue, "(?<=: )\d+").Value
Also have a look here:
This CheatSheet introduces the basic use of regex functions. With further examples also special cases are presented.
Introduction
From the namespace System.Text.RegularExpressions following methods are offered:
Regex.Match
Regex.Matches
Regex.isMatch
Regex.Replace
Regex.Split
A simple usage for example would look like this:
[grafik]
Recommendation:
add System.Text.RegularExpressions to the imports:
[grafik]
it allows to use the shortened statement, as the namespace part can be ommited…
mkankatala
(Mahesh Kankatala)
July 30, 2023, 8:28pm
7
Yes @Hugen_Dugen
You have to use assign for this
- Assign -> MatchVar = System.Text.RegularExpressions.Regex.Matches(yourstringinput.ToString,“((?<=Your Orderd verification number is\:\s+)\d+)”)
In the above assign MatchVar is a Ienumerable of Matches datatype variable.
In the place of yourstringinput.ToString provide the String variable which has the input text.
After this take an one more assign activity. Then create a variable to change Ienumerable of matches to text as below.
- Assign -> Output = MatchVar(0).toString
Or
With out using assign you can use another activity called find matching patterns activity to get the output by using regex.
You can copy the above regex expression and paste it in the find matching pattern activity by changing to advance. You can pass the input data string variable. Create a variable in output by ctrl+k. Then take an assign activity after that to take the output as string.
- Assign -> Output = MatchVar(0).toString
Output has to be String datatype variable.
Hope it helps!!
mkankatala
(Mahesh Kankatala)
July 30, 2023, 8:37pm
9
@ppr yeah sure I’ll do note of it.
lrtetala
(Lakshman Reddy)
July 30, 2023, 8:55pm
10
Hi @Hugen_Dugen
You can simply try this
1.Use Deserialise JSON activity
2.Use Assign activity
VerificationNumber=myJObject(“activeActivations”)(0)(“smsCode”)(0).Value(Of String)
Regards,
Thank u can u give me little hint what i have to input here
:VerificationNumber=myJObject(“activeActivations”)(0)(“smsCode”)(0).Value(Of String)
mkankatala
(Mahesh Kankatala)
July 30, 2023, 9:05pm
12
@Hugen_Dugen
Verification number is the String datatype variable.
In the save to field of assign activity create a variable called verification number. In the value to field of assign activity give the remaining expression. Like below
- Assign -> VerificationNumber=myJObject(“activeActivations”)(0)(“smsCode”)(0).Value(Of String)
After this also you have to use the above regular expressions to get the required output. Here you are saving one object into a variable.
Hope you understand!!
lrtetala
(Lakshman Reddy)
July 30, 2023, 9:09pm
13
@Hugen_Dugen
First take Deserialize JSON activity create a output variable of that activity as myJObject
then take assign activity
VerificationNumber=myJObject(“activeActivations”)(0)(“smsCode”)(0).Value(OfString)
ty I did the desterilization already.
But as in ur Screenshot i have the expression mark as well. So there is an error isnt it?
lrtetala
(Lakshman Reddy)
July 30, 2023, 9:22pm
15
@Hugen_Dugen
Try this
myJObject(“activeActivations”)(0)(“smsCode”). ToString
lrtetala
(Lakshman Reddy)
July 30, 2023, 9:31pm
16
If that is not working please try this
JSONObject("activeActivations")(0)("smsCode")(0).ToString().Split(":"c)(1).Trim()
lrtetala
(Lakshman Reddy)
July 30, 2023, 9:36pm
18
@Hugen_Dugen
Can u please provide json file
Try this
myJObject(“activeActivations”)(0)(“smsCode”)(0).Value(Of int32)
DUgen.txt (1.4 KB)
Of Course. Thank u for ur hel appreciate it.
ppr
(Peter Preuss)
July 30, 2023, 9:43pm
20
Hugen_Dugen:
Value(Of String)
With the Value Method we grab the value as string
As we get
we got more then only the number
After using regex we get the number as string:
when the extracted number string is needed as an int we can do
myCodeAsInt | DataType: int32 = CInt(myCode )