Convert string to integer inside body data in jmeter - json

I stored jobID in variable as newID after create a job ,I try to pass this newID as integer ,I want to convert it as integer because when i create a job it return string ID ?
{"limit":100,"offset":0,"countryID":null,"experienceYear":null,"languageIdIn":[],"degreeLevel":null,"jobID":"${__groovy(newID)}"]}
```

In JSON you need to remove the quotes around integer value
jobID":${newID}

Your request body is not a valid JSON, you need to remove this unbalanced ], you can check it yourself using online JSON validator tool
You need to remove quotation marks around jobID attribute value, see JSON Data Types for more details
${__groovy(newID)} expression is syntactically incorrect, if newID is a JMeter Variable - you need to refer it using vars shorthand for JMeterVariables class like:
Putting everything together:
{
"limit": 100,
"offset": 0,
"countryID": null,
"experienceYear": null,
"languageIdIn": [
],
"degreeLevel": null,
"jobID": ${__groovy(vars.get('newID'),)}
}

Related

How can I use the oracle REGEXP_SUBSTR to extract specific json values?

I have some columns in my Oracle database that contains json and to extract it's data in a query, I use REGEXP_SUBSTR.
In the following example, value is a column in the table DOSSIER that contains json. The regex extract the value of the property client.reference in that json
SELECT REGEXP_SUBSTR(value, '"client"(.*?)"reference":"([^"]+)"', 1, 1, NULL, 2) FROM DOSSIER;
So if the json looks like this :
[...],
"client": {
"someproperty":"123",
"someobject": {
[...]
},
"reference":"ABCD",
"someotherproperty":"456"
},
[...]
The SQL query will return ABDC.
My problem is that some json have multiple instance of "client", for example :
[...],
"contract": {
"client":"Name of the client",
"supplier": {
"reference":"EFGH"
}
},
[...],
"client": {
"someproperty":"123",
"someobject": {
[...]
},
"reference":"ABCD",
"someotherproperty":"456"
},
[...]
You get the issue, now the SQL query will return EFGH, which is the supplier's reference.
How can I make sure that "reference" is contained in a json object "client" ?
EDIT : I'm on Oracle 11g so I can't use the JSON API and I would like to avoid using third-party package
Assuming you are using Oracle 12c or later then you should NOT use regular expressions and should use Oracle's JSON functions.
If you have the table and data:
CREATE TABLE table_name ( value CLOB CHECK ( value IS JSON ) );
INSERT INTO table_name (
value
) VALUES (
'{
"contract": {
"client":"Name of the client",
"supplier": {
"reference":"EFGH"
}
},
"client": {
"someproperty":"123",
"someobject": {},
"reference":"ABCD",
"someotherproperty":"456"
}
}'
);
Then you can use the query:
SELECT JSON_VALUE( value, '$.client.reference' ) AS reference
FROM table_name;
Which outputs:
REFERENCE
ABCD
db<>fiddle here
If you are using Oracle 11 or earlier then you could use the third-party PLJSON package to parse JSON in PL/SQL. For example, this question.
Or enable Java within the database and then use CREATE JAVA (or the loadjava utility) to add a Java class that can parse JSON to the database and then wrap it in an Oracle function and use that.
I faced similar issue recently. If "reference" is a property that is only present inside "client" object, this will solve:
SELECT reference FROM (
SELECT DISTINCT
REGEXP_SUBSTR(
DBMS_LOB.SUBSTR(
value,
4000
),
'"reference":"(.+?)"',
1, 1, 'c', 1) reference
FROM DOSSIER
) WHERE reference IS NOT null;
You can also try to adapt the regex to your need.
Edit:
In my case, column type is CLOB and that's why I use DBMS_LOB.SUBSTR function there. You can remove this function and pass column directly in REGEXP_SUBSTR.

How to differentiate the explicitly assigning null to the field or field that is not in the json file for #PATCH

I am using #PATCH for the partial update of the record.
My table is emp:
empId - Int
lastName -String
firstName - String
city - String
desc - String
Patch json file for emp/1:
{"city" : "NULL", "lastName": "newLastName"}
How can I pass this to the pl/sql procedure (how the sql query can be constructed only to update city and lastName) and how to differentiate between explicity city set to NULL and desc is not found in json.
There are multiple ways on how to deal with that kind of situation, I implemented at least two different ones:
Send a JSON document that represents the updated resource
Either you require the client to always send the complete JSON document (not a partial one) so you always get all values in a single request (easy). Absent values mean that the field is supposed to be null.
Or you allow the client to send a partial JSON document but then you need a unique identifier to distinguish between an absent field (not to touch) and a field that needs to be overridden with null (harder but not impossible).
Send a JSON document that contains operations on a resource, e. g. { "operation": "update", "field": "city", "value": "New City" } which replaces the old value of city with New City or { "operation": "delete", "field": "description" } which deletes description (requires parsing patches differently).

Cannot parse JSON String data to Integer

Suppose my JSON is like this.
// {
// "count": 32,
// "weight": 1.13,
// "name": "grape",
// "isFruit": true
// "currentPrice" : "30.00"
// }
If I read my JSON like this,
String current = json.getString("currentPrice");
the current variable will have value as "30.00". Is there any way that I can parse this as an Integer? I tried doing Integer.parseInt but It is giving an error like Number format exception for input string "30.00".
I tried removing quotes by applying regex but didn't work.
You need to use
parseInt('current')
parseInt(num); // default way (no radix)
parseInt(num, 10); // parseInt with radix (decimal)
parseFloat(num) // floating point
Number(num); // Number constructor
to get current
You want parseFloat(). 30.00 isn't an integer, even though it's numerically EQUAL to the integer 30.
If you want it as an integer, you can use Math.floor() to convert it to one, or you can use parseInt() to get the integer portion, but if you really want the whole value (if it might not always be whole), parse it as a float.

OpenEdge ABL reserved keyword as temp-table field name (inferred from JSON data)

I am stuck with the following situation:
My method receives a response from external REST API call. The JSON response structure is as below:
{
"members": [
{
"email_address": "random#address.org",
"status": "randomstatus"
},
...etc...
]}
I am reading this to temp-table with READ-JSON (Inferring ABL schema from JSON data) and try to process the temp-table. And this is where I am stuck:
when I am trying to put together a query that contains temp-table field "status", the error is raised.
Example:
hQuery:QUERY-PREPARE('FOR EACH ' + httSubscriber:NAME + ' WHERE ' + hBuffer:BUFFER-FIELD(iStatus):NAME + ' = "randomstatus"').
gives:
**Unable to understand after -- "members WHERE".(247)
I have tried referencing directly by name as well, same result.
Probably the "status" is a reserved keyword in ABL. Might that be the case? And how can I get over this issue to reference that "status" field?
Unfortunately the format and key names of JSON response are not under my control and I have to work with that.
You could use SERIALIZE-NAME in the temp-table definition to internally rename the field in question. Then you would have to refer to the field with another name but in it's serialized form it would still be known as status.
Here's an example where the status-field is renamed to exampleStatus.
DEFINE TEMP-TABLE ttExample NO-UNDO
FIELD exampleStatus AS CHARACTER SERIALIZE-NAME "status".
/* Code to read json goes here... */
/* Access the field */
FOR EACH ttExample:
DISPLAY ttExample.exampleStatus.
END.
I've been known to do silly things like this:
JSONData = replace( JSONData, '"status":', '"xstatus":' ).
Try naming the temp-table (hard-coded or via string appending) + '.' + hBuffer:BUFFER-FIELD(iStatus):NAME (...)
It should help the compiler understand you're talking about the field. Since it's not restricted, this should force its hand and allow you to query.

receiving string literals and storing it in the integer column in the database

Sendgrid says that the Categories are in the form of String data type as follows:
[
{
"email": "john.doe#sendgrid.com",
"timestamp": 1337966815,
"category": [
"newuser",
"transactional"
],
"event": "open"
},
{
"email": "jane.doe#sendgrid.com",
"timestamp": 1337966815,
"category": "olduser"
"event": "open"
}
]
So, if someone sends a numerical number , say 11111 for category, sendgrid will treat it as a string and send it back .
I always use numerical values to send and when I get the same value back, I store it in an integer column in my database. For example I will be storing
11111 value in my column category_int and it's data type is int(11).
Since, I am always storing it in an integer column, whenever someone sends category in the form of actual string just like above olduser , I get an error.
My Question:
Am I seeing error because,my category_int column is intended to receive integer values and not string values? But if this is true then, isn't the value that sendgrid is sending , in case of numbers(in my case 11111) is
treated as string and perhaps, I should be getting error in this case as well. Where as I get errors only when I send category in the form of some descriptive words like olduseretc.
Please advise.
The problem is that an int(11) column cannot contain a string. MySQL is able to understand that "11111" as a string can be changed to the int 11111 and knows how to convert it. But it doesn't know how to make olduser into an int, so it throws an error.
You could change your data type to varchar, and then that column would be able to handle both 11111 and olduser.
You could also check the value in your code before you pass it to the database, and throw out values that cannot be made into ints.