Get specific value from OData response in Postman - postman

Can anyone of you can help me this:
i Have such response body from WebAPI:
{
"value": "Created: \"salesorder\" : \"22a734c3-bf5f-ec11-80e6-0050568d2958\"
Found 0 vis1_anschlussadresses
Found 1 vis1_postleitzahls
Reusing: \"vis1_postleitzahl\" : \"0d9344c7-a45d-e711-80c5-955c5ca2a164\"
Found 1 vis1_orts
Reusing: \"vis1_ort\" : \"92734f57-375e-e711-80c5-955c5ca2a164\"
Created: \"vis1_anschlussadresse\" : \"67a734c3-bf5f-ec11-80e6-0050568d2958\"
Found 0 vis1_anschlussobjekts
Created: \"vis1_anschlussobjekt\" : \"6ba734c3-bf5f-ec11-80e6-0050568d2958\"
Found 0 vis1_infrastrukturinformations
Created: \"vis1_infrastrukturinformation\" : \"6fa734c3-bf5f-ec11-80e6-0050568d2958\"
Found 1 contacts
Reusing: \"contact\" : \"22530f60-285f-ec11-80e6-0050568d2958\"
Found 1 competitors
Reusing: \"competitor\" : \"7841f8e7-c211-ea11-80cd-0050568d3968\"
[0000]: Information: OK
"
}
i'd like to get specific values from this response body e.g. 22a734c3-bf5f-ec11-80e6-0050568d2958 and store as a environment variable. Is it possible?

The first observation is that the response is a standard serialized structure. Even if we deserialise the JSON payload, the value of value is this string, that seems to represent a form of execution log:
Created: "salesorder" : "22a734c3-bf5f-ec11-80e6-0050568d2958"
Found 0 vis1_anschlussadresses
Found 1 vis1_postleitzahls
Reusing: "vis1_postleitzahl" : "0d9344c7-a45d-e711-80c5-955c5ca2a164"
Found 1 vis1_orts
Reusing: "vis1_ort" : "92734f57-375e-e711-80c5-955c5ca2a164"
Created: "vis1_anschlussadresse" : "67a734c3-bf5f-ec11-80e6-0050568d2958"
Found 0 vis1_anschlussobjekts
Created: "vis1_anschlussobjekt" : "6ba734c3-bf5f-ec11-80e6-0050568d2958"
Found 0 vis1_infrastrukturinformations
Created: "vis1_infrastrukturinformation" : "6fa734c3-bf5f-ec11-80e6-0050568d2958"
Found 1 contacts
Reusing: "contact" : "22530f60-285f-ec11-80e6-0050568d2958"
Found 1 competitors
Reusing: "competitor" : "7841f8e7-c211-ea11-80cd-0050568d3968"
[0000]: Information: OK
To specifically resolve the value 22a734c3-bf5f-ec11-80e6-0050568d2958 we need to make a few assumtions:
the value we are looking for is in the First line
the value is contained inside double quotes
the quoted value is the entire line content after the last full-colon character ;
the name of the environment variable is FirstGuid
Given those assumptions, we can write a Tests script to run after the request:
var logValue = pm.response.json().value;
var lines = logValue.split(/\r?\n/);
var lineOneTokens = lines[0].split(':');
var guid = lineOneTokens[lineOneTokens.length() - 1].trim().replace(/"/g,"");
pm.environment.set('FirstGuid', guid);
References:
Scripting in Postman
JavaScript - split string by new line character
How to Get the Last Element of an Array in JavaScript
I want to remove double quotes from a String

Related

writing a logic to check if value exists

working on the data returned by code
Trying to add some logic that if the value exists, show it else put empty
<cfset myStruct = {
"access_token" : "#st.access_token#",
"id": "#res.names[1].metadata.source.id#",
"name" : "#isDefined('res.names') ? res.names[1].displayname : ''#",
"other" : {
"email" : "#res.emailAddresses[1].value#"
}
}>
Open in new window
its not clean and it throws error on line 3 which is ID, so what kind of isDefined or structkeyexists i can write if it exists add it, else put an empty value
You could try Elvis operator
Edit: Unless you really need the values as a string, you do not need to use pounds to output the values
Edit 2: Have updated the example to use the right comment
<cfset myStruct = {
"access_token" : "#st.access_token#" <!--- If you have numeric token and need it to be a string --->
, "id" : res.names[ 1 ].metadata.source.id ?: ""
, "name" : res.names[ 1 ].displayname ?: ""
, "other" : {
"email" : res.emailAddresses[ 1 ].value ?: ""
}
}>

in Google Apps Script; using Regular Expression & Conditional formatting - to create a IPv4 address validator / checker

in Google Apps Script; using Regular Expression & Conditional formatting - to create a IPv4 address validaton / checker
1. VALIDATING the input with regular expression :
I try to make a script to get feedback of a validator, to check if the input an IP addresses is.
regular expression pattern :
cell ' ip!I12 ' named ' REGEXP_IP_pattern ' :
^((((25[0-5])|(2[0-4][0-9])|([01]?[0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|([01]?[0-9]{1,2})))
reg exp demo : https://regexr.com/54fon - works fine (in the formula)
formula in the cell :
works in the formula fine, but I have to work with formulas in formulas to define if the status is valid or not.
The regular expression in formula format works fine [see worksheet 'ip'], but the sad thing is that I fail to get it working in the script.
Have some issues to retrieve the correct and good information, to define if a input 'ip' is valid or not. (see column 'input' , 'good ip')
in the formula :
=IF( REGEXREPLACE(TRIM( <cell> &"");REGEXP_IP_pattern;wildChar_IP) = wildChar_IP;1;0)
to force and cast the cell to a string : TRIM( <cell> &"")
'remove' the founded regexpmatch and on the rest of the string add a 'wildChar_IP' [a wild character]
check if the rest is 'nothing' (in our case equal to the 'wildChar_IP')
script / code :
For the Google Apps Script, TOOLS > SCRIPT EDITOR
To see the output of the logger : VIEW > LOGBOOK
So how is it possible to get feedback, status, if it only matches with regular expression in a function?
for example a boolean as return.
This is my code ... but fails to define when it really matches.
Because of autosense/autocomplete is not really reliable, is there an other way to find out all method of
var regExp = new RegExp(regExpPattIP, "gi");
var result01 = regExp.exec(ip01);
code :
var ip01 ='256.12.2.0'
var ip02 ='255.12.2.0'
var ip03 ='192.168..1'
var ip04 ='10.12.12.12.12'
var ip05 ='010.060.090.002'
function validatorRegExp() {
//var regExpPattIP ="^((((25[0-5])|(2[0-4][0-9])|([01]?[0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|([01]?[0-9]{1,2})))";
var regExpPattIP = actSheet.getRangeByName(RegExp_patt_IP).getValue();
var regExp = new RegExp(regExpPattIP, "gi"); // "i" is for case insensitive -- "g" is for global
var result01 = regExp.exec(ip01);
console.log(" --- ip01: ", ip01, "---");
console.log(result01);
var result02 = regExp.exec(ip02);
console.log(" --- ip02: ", ip02, "---");
console.log(result02);
var result03 = regExp.exec(ip03);
console.log(" --- ip03: ", ip03, "---");
console.log(result03);
var result04 = regExp.exec(ip04);
//var result04B = regExp.exec(ip04)[1]; // null - PROBLEM
console.log(" --- ip04: ", ip04, "---");
console.log(result04);
// console.log(result04B);
console.log(" --- ip05: ", ip05, "---");
var result05 = regExp.exec(ip05);
var result05B = regExp.exec(ip05)[1];
console.log(result05);
console.log(result05B);
}
RESULT
[20-05-12 23:54:45:052 CEST] --- ip01: 256.12.2.0 ---
[20-05-12 23:54:45:054 CEST] null
[20-05-12 23:54:45:056 CEST] --- ip02: 255.12.2.0 ---
[20-05-12 23:54:45:058 CEST] [ '255.12.2.0',
'255.12.2.0',
'2.',
'2',
undefined,
undefined,
'2',
'0',
undefined,
undefined,
'0',
index: 0,
input: '255.12.2.0',
groups: undefined ]
[20-05-12 23:54:45:060 CEST] --- ip03: 192.168..1 ---
[20-05-12 23:54:45:061 CEST] null
[20-05-12 23:54:45:063 CEST] --- ip04: 10.12.12.12.12 --- ****** is bad
[20-05-12 23:54:45:064 CEST] [ '10.12.12.12',
'10.12.12.12',
'12.',
'12',
undefined,
undefined,
'12',
'12',
undefined,
undefined,
'12',
index: 0,
input: '10.12.12.12.12',
groups: undefined ]
[20-05-12 23:54:45:066 CEST] --- ip05: 010.060.090.002 ---
[20-05-12 23:54:45:068 CEST] null
[20-05-12 23:54:45:069 CEST] 010.060.090.002
If you think a 'valid' feature would be helpfull, vote up here : https://issuetracker.google.com/issues/36762591
2. VISUALISING the 'valid' / 'unvalid' *status* of the input with conditional formatting :
And the idea is then make it visual by 'conditional formatting'
(https://developers.google.com/sheets/api/guides/conditional-format#apps-script)
All Help is more then welcome.
formula and script:
Link the spreadsheet and google script :
https://drive.google.com/open?id=1HTgdC6Ss8oOvwVm86kbOdlt5jX8UyVmd-Eyo-oWhEGw
(only read access, if you want to test/check it out make a copy)
REMARK : (continued)
How can I dynamically apply a regular expression on change of a cell or in conditional format?
Google Apps Script; using Regular Expression on a dynamic way implemented, on change of a cell or in conditional format.
I believe your goal as follows.
You want to output the boolean type when the IP addresses are checked using Google Apps Script.
You want to check a column in the Spreadsheet.
For this, how about this answer?
Modification points:
In this answer, as the regex for matching IPV4, ^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ is used. Ref
In order to check and return the boolean type, I used test() for this.
Sample script:
The sample script is as follows. In this case, it supposes that there are the IP addresses in the column "A". And the output value is put to the column "B".
function myFunction() {
const regExpPattIP = '^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$';
const regExp = new RegExp(regExpPattIP);
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
const range = sheet.getRange("A1:A" + sheet.getLastRow());
const values = range.getDisplayValues();
var res = values.map(([e]) => ([regExp.test(e.trim())]));
range.offset(0, 1).setValues(res);
}
Note:
When the following modification is reflected to above sample script, the cell color of the valid IP address is changed.
From
var res = values.map(([e]) => ([regExp.test(e.trim())]));
range.offset(0, 1).setValues(res);
To
var colors = values.map(([e]) => ([regExp.test(e.trim()) ? "green" : null]));
range.setBackgrounds(colors);
Result:
When above script is run, the following result is obtained. I used your sample IP addresses at the column "A". The result is put to the column "B".
References:
test()
trim()

How to parse txt-file using REGEXP_SUBSTR in oracle?

I have got a lot of txt cards with same format.
And I need parse it to get some values from them.
I don't understand how use regexp substr in Oracle. Please, Help me write sql statements, which return values, which I marked between **-symbols (for example: first string, 02/02/11, AA11223344 and etc):
From: "abc def (**first string**)" <email#site.com>
02/01/2011 09:27 First Date : **02/02/11**
Player : BILL BROWN ID : **AA11223344**
At : YELLOW STREET. CD Number : **A11223FER**
Code :
BUYS : **123M** (M) of AAA 0 02/02/11 Owner : **England**
Shiped : **02/04/11**
Number : **11.223344** Digi : **1.2370000**
Ddate: **02/04/11**
Notes : **First line here**
* Size : **USD 11,222,333.44**
* Own ( **0 days** ): **0.00**
* Total : USD **222,333,444.55**
You can recursively apply regexp evaluation by using hierarchical queries; at each level, you look for the level-th occurrence in your string.
Please pay attention to the "non greedy" operator (??) in pattern string, explained here, as well as regular expression functions.
with test as (
select 'From: "abc def (**first string**)" <email#site.com>
02/01/2011 09:27 First Date : **02/02/11**
Player : BILL BROWN ID : **AA11223344**
At : YELLOW STREET. CD Number : **A11223FER**
Code :
BUYS : **123M** (M) of AAA 0 02/02/11 Owner : **England**
Shiped : **02/04/11**
Number : **11.223344** Digi : **1.2370000**
Ddate: **02/04/11**
Notes : **First line here**
* Size : **USD 11,222,333.44**
* Own ( **0 days** ): **0.00**
* Total : USD **222,333,444.55** ' as txt
from dual
)
select TRIM('*' FROM regexp_substr(txt, '\*\*(.*??)\*\*', 1, LEVEL, 'n') )
from test
CONNECT BY regexp_subSTR(txt, '\*\*(.*??)\*\*', 1, LEVEL, 'n') is not null

Can we extract dyanmic data from string using regex?

I want to validate and get the data for following tags(9F03,9F02,9C ) using regex:
9F02060000000060009F03070000000010009C0101
Above string is in Tag - length - value format.
Where 9F02,9F03,9C are tags and have fixed length but their position and value in string can vary.
Just after the tag there is the length of the value in bytes that tag can store.
for example:
9F02=tag
06=Length in bytes
000000006000= value
Thanks,
Ashutosh
Standard regex doesn't know how to count very well, it behaves like a state machine in that way.
What you can do though if the number of possibilities is small is represent each possibility in a state in regex, and use multiple regex queries for each tag ...
/9F02(01..|02....|03......)/
/9C(01..|02....)/
... And so on.
Example here.
http://rubular.com/r/euHRxeTLqH
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegEx {
public static void main(String[] args) {
String s = "9F02060000000060009F03070000000010009C0101";
String regEx = "(9F02|9F03|9C)";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(s);
while(m.find()){
System.out.println("Tag : "+ m.group());
String length = s.substring(m.end(), m.end()+2);
System.out.println("Length : " + length);
int valueEndIndex = new Integer(m.end()) + 3 + new Integer(length);
String value = s.substring(m.end()+3,valueEndIndex);
System.out.println("Value : "+ value);
}
}
}
This code will give you following output :
Tag : 9F02
Length : 06
value : 000000
Tag : 9F03
Length : 07
value : 0000000
Tag : 9C
Length : 01
value : 1
I am not sure about byte length you are mentioning here, but I guess this code shall help you kick start!

Powershell: How to Validate Range between 1 to 15 and it should accepts pattern as 1,2,3,4,5 to 15

I am a newbie in Power Shell Scripting.
I am trying to Achieve a functionality, that should accepts inputs from user in below criteria
Only Digits
Range Between 1 to 15
Should accept String of Array with comma Separated values ex: 1,2,3,4,14,15
Can Contain space in between commas
Values should not be duplicated
The Returned values must be Array
Till now, I have tried
Function Validate-Choice{
[cmdletbinding()]
Param(
[Parameter(Position=0,Mandatory=$True)]
[ValidateRange(1,15)]
[string[]]$Item
)
Process {$Item}
}
Validate-Choice 1,2,3,4,5,6,7,8,9,10,11,13 # Similar Way i want O/p
Out Put:
1
2
3
4
5
6
7
8
9
10
11
13
$ReadInput = Read-Host -prompt "Please Choose from the list [1/2/3/4/5/6/7/8/9/10/11/12/13/14] You can select multiple Values EX: 1, 2, 3 -- "
$userchoices = Validate-Choice -item $ReadInput
$userchoices
If read the same input from Host Getting Below Error
Validate-Choice : Cannot validate argument on parameter 'Item'. The argument cannot be validated because
its type "String" is not the same type (Int32) as the maximum and minimum limits of the parameter. Make sure the argument is of type Int32 and then try the command again. At line:10 char:21
+ Validate-Choice '1,2,3,4,5,6,7,8,9,10,11,13'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Validate-Choice], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Validate-Choice
And also i am trying with different Regex patterns. But failing
Function Test-Something {
[cmdletbinding()]
Param(
[Parameter(Position=0,Mandatory=$True)]
[ValidatePattern('(?:\s*\d{1,15}[1-15]\s*(?:,|$))+$')]
[string[]]$Item
)
Process { $Item }
}
The above functions are partially resulting.
Can any one please help me here..!?
This would probably be easiest if you just changed the parameter type to [int[]] then your ValidateRange attribute does most of the work. It doesn't handle duplicates though. Turns out you can't use [ValidateScript()] as #PetSerAl points out. So that leaves checking the parameter the old fashioned way, in a begin block:
Function Test-Something {
[cmdletbinding()]
Param(
[Parameter(Position=0, Mandatory)]
[ValidateRange(1, 15)]
[int[]]$Item
)
Begin {
$ht = #{}
foreach ($i in $Item) {
if ($ht.ContainsKey("$i")) {
throw "Parameter Item must contain unique values - duplicate '$i'"
}
else {
$ht["$i"] = $i
}
}
}
Process {
[string]$Item
}
}
Test-Something (1,2,3,4,3)
Note that this won't work if you make the Item parameter accept pipeline input. In the pipeline input case, $Item will not be set in the begin block.
Naren_Ch
Your 1st usage of the advanced function (AF)
Validate-Choice 1,2,3,4,5,6,7,8,9,10,11,13 # Similar Way i want O/p
is correct - you are inputting data as expected by the AF.
Now, look at the example reading the input from the host:
$ReadInput = Read-Host -prompt "Please Choose from the list [1/2/3/4/5/6/7/8/9/10/11/12/13/14] You can select multiple Values EX: 1, 2, 3 -- "
When you do this, $ReadInput is a string, and in this case, it's a string full of commas!
Consequently, your data inputted to the AF will result in error caused by validation code, written by yourself.
To correct the situation, just do this:
$ReadInput = (Read-Host -prompt "Please Choose etc...") -split ','
$userchoices = Validate-Choice -item $ReadInput
You must remember that data read by Read-Host is a string (just 1 string).