beanShell json body for post method on Jmeter - beanshell

can anyone help to add escape quotation marks with slash like \" on this son body:
{
"firstName": "teo",
"lastName": "leo",
"companyName": "abc",
"restaurantId": "54d34443e4b0382b3208703d",
"phones": [
{
"label": "Mobile",
"value": "123456789",
"countryCode": "+123",
"isPrimary": true
}
],
"addresses": "haha"
}
i've tried with this one but beanShell PreProcessor can't accept it
String formvalues = "{\"firstName\": \"teo\",\"lastName\": \"leo\",\"companyName\": \"abc\",\"restaurantId\": \"54d34443e4b0382b3208703d\",\"phones\": [{\"label\":\"Mobile\",\"value\": \"123456789\",\"countryCode\": \"+123\",\"isPrimary\": true}],\"addresses\": \"haha\"}"
thanks you so much!

If you want to keep formatting:
String formvalues = "{\n" +
" \"firstName\": \"teo\",\n" +
" \"lastName\": \"leo\",\n" +
" \"companyName\": \"abc\",\n" +
" \"restaurantId\": \"54d34443e4b0382b3208703d\",\n" +
" \"phones\": [\n" +
" {\n" +
" \"label\": \"Mobile\",\n" +
" \"value\": \"123456789\",\n" +
" \"countryCode\": \"+123\",\n" +
" \"isPrimary\": true\n" +
" }\n" +
" ],\n" +
" \"addresses\": \"haha\"\n" +
"}";
If you want single line (mind that Content-Length will be different)
String formvalues = "{\"firstName\":\"teo\",\"lastName\":\"leo\",\"companyName\":\"abc\",\"restaurantId\":\"54d34443e4b0382b3208703d\",\"phones\":[{\"label\":\"Mobile\",\"value\":\"123456789\",\"countryCode\":\"+123\",\"isPrimary\":true}],\"addresses\":\"haha\"}";
Full code to generate the body and add it as parameter:
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.http.util.HTTPArgument;
String formvalues = "{\n" +
" \"firstName\": \"teo\",\n" +
" \"lastName\": \"leo\",\n" +
" \"companyName\": \"abc\",\n" +
" \"restaurantId\": \"54d34443e4b0382b3208703d\",\n" +
" \"phones\": [\n" +
" {\n" +
" \"label\": \"Mobile\",\n" +
" \"value\": \"123456789\",\n" +
" \"countryCode\": \"+123\",\n" +
" \"isPrimary\": true\n" +
" }\n" +
" ],\n" +
" \"addresses\": \"haha\"\n" +
"}";
Arguments arguments = new Arguments();
arguments.addArgument(new HTTPArgument("",formvalues));
sampler.setArguments(arguments);
JavaDoc on relevant classes:
Arguments
HTTPArgument
HTTPSamplerProxy (shorthand for sampler)
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on Beanshell scripting in JMeter.

Related

JOIN 2 tables based on req.query in a 3rd table

I have 3 tables (deals, deals_media, access_to_deals)
deals deals_media access (to deals)
name pic_url type media_url deal_id customer_id deal_id
deal1 http://blah video url. 1 1 1
deal2 http://blah2 img url2. 1 1 2
deal3 http://blah3 video url3. 2 2 1
I'm sending an api get request with a customer_id query parameter.
I want to send back an array of objects with all the deals data the customer is eligible for and to join an array of matching deals_media to the deals.id
I'm using sequalize and await functions and my query looks like:
sync (req, res) => {
try {
const customerId = req.query.customer_id;
let deals = await db.sequelize.query(
'SELECT DISTINCT deals.* FROM deals \n' +
'JOIN access ON deals.id = access.deals_id AND access.customerId=$1 \n' +
'LEFT JOIN media ON deals.id = media.deals_id \n',
{ bind: [customerId], type: 'RAW' },
);
res.send(campaign[0]);
} catch (err) {...}
};
my response data looks like:
{
"id":1,
"deal_name": "deal1",
"other data": "data",
"media type": "video",
"media URL": "https://www.......",
"deals_id": 1
},
{
"id":2,
"deal_name": "deal1",
"other data": "data",
"media type": "picture",
"media URL": "https://www.......",
"deals_id": 1
},
{
"id":3,
"deal_name": "deal1",
"other data": "data",
"media type": "audio",
"media URL": "https://www.......",
"deals_id": 1
}
]
and I'd prefer it came out as:
{
"id":1,
"deal_name": "deal1",
"other data": "data",
"media": [
{
"media type": "video",
"media URL": "https://www......."
},
{
"media type": "picture",
"media URL": "https://www......."
},
{
"media type": "audio",
"media URL": "https://www......."
}
]
I've tried to change the last join to 'LEFT JOIN media ON deals.id = media.deals_id GROUP BY media.deals_id \n' however I get an error to include deals.id and media.id to the GROUP BY and then it produces the same incorrect query result as above. I'm relatively new to SQL so any direction is greatly appreciated. Many thanks for considering my request.
UPDATE: As to #NBK's suggestion, I've updated the query below however I'm running into the following error Error is Deals: SequelizeDatabaseError: schema "deals" does not exist
'SELECT json_build_object ( \n' +
"'id', deals.id \n" +
"'deals_name', deals.deals_name \n" +
"'deals_icon_url', deals.deals_icon_url \n" +
"'conversion_event', deals.conversion_event \n" +
"'created_at', daels.created_at \n" +
"'updated_at', deals.updated_at \n" +
"'deleted_at', deals.deleted_at \n" +
"'media', json_build_array ( \n" +
"json_build_objecty ( \n" +
"'media_type', media.media_type \n" +
"'media_url', media.media_url \n" +
') \n' +
') \n' +
') \n' +
'FROM deals \n' +
'INNER JOIN access ON access.deals_id = deals.id AND access.user_id=$1\n' +
'INNER JOIN media ON media.deals_id = deals.id \n',

Mule DWL 2.0 to convert number to word

Input : 123
output: one hundred and twenty three
how to convert number to word in dwl
This answer has been inspired from a Java solution to the problem at hand. However this is not generic enough and solves only for 3 digit numbers. You can definitely extend this approach though.
Script
%dw 2.0
output json
import * from dw::Runtime
var units = [
"",
" one",
" two",
" three",
" four",
" five",
" six",
" seven",
" eight",
" nine"
]
var twoDigits = [
" ten",
" eleven",
" twelve",
" thirteen",
" fourteen",
" fifteen",
" sixteen",
" seventeen",
" eighteen",
" nineteen"
]
var tenMultiples = [
"",
"",
" twenty",
" thirty",
" forty",
" fifty",
" sixty",
" seventy",
" eighty",
" ninety"
]
var placeValues = [
" ",
" thousand",
" million",
" billion",
" trillion"
]
var inputNum = 413
fun wtonum(inp) = do {
var word =""
var index=0
---
if((inp mod 1000) != 0)
{ a: (conv(inp mod 1000) ) }
else ""
}
fun conv(numInp) = do {
var words = ""
var calc = if((numInp mod 100) < 10) {a: units[numInp]} else if ((numInp mod 100) < 20) {a: twoDigits[(numInp mod 100) mod 10]} else {a: (tenMultiples[(numInp mod 100)/10] ++ units[(numInp mod 100) mod 10])}
---
if(numInp/100 > 0.99) units[numInp/100] ++ " hundred" ++ calc.a else calc.a
}
---
wtonum(inputNum)

How to search Host Groups in Zabbix API

I want to list all Host Groups that match some search criteria.
I've tried that:
data = '{"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
"search": {
"name": [
"' + group_name + '"
]
},
},
"id":' + str(msg_id) + ',
"auth": "' + auth + '"
}'
But that is not a correct syntax.
I also tried this:
data = '{"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
"filter": {
"name": [
"' + group_name + '"
]
},
},
"id":' + str(msg_id) + ',
"auth": "' + auth + '"
}'
This one works, but it only matches exactly the group name. And, so, it always returns 1 or 0 matches.
I tried adding the "options":"searchWildcardsEnabled" option in this last query, but it didn't make a difference in the result (i.e. it didn't produce multiple groups as output).
I've found the correct way. I'll post it here in case anyone else needs it later.
data = '{"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
"search": {
"name": [
"' + group_name + '"
]
}
},
"id":' + str(msg_id) + ',
"auth": "' + auth + '"
}'
You don't need to specify the wildcard, it's default. Also, you don't need to put the % inside your query.

Transform PDI response to determined structure

I'm new to PDI I'm working recovering information from an API and I need to transform the information that comes to a certain structure and I'm not clear how to do it with the concatenation or with another transformation.
This is my answer to work with:
[
  {
    "457": {
      "1": {
        "value": "4.1",
        "timestamp": 1534159593
      },
      "2": {
        "value": "52.2",
        "timestamp": 1534159593
      },
      "3": {
        "value": "23.0",
        "timestamp": 1534159593
      },
      "4": {
        "value": "250.0",
        "timestamp": 1534159593
      }
    }
  }
]
and I would need something of this type to remain, to send it by POST to another API
{
  "id": "457",
  "type": "greenhouse",
  "1": {
    "value": 4.1,
    "type": "Float",
    "timestamp": 1534159593
  },
  "2": {
    "value": 52.2,
    "type": "Integer",
    "timestamp": 1534159593
  },
  "3": {
    "value": 23.0,
    "type": "Integer",
    "timestamp": 1534159593
  },
  "4": {
    "value": 250.0,
    "type": "Integer",
    "timestamp": 1534159593
  }
}
Thanks for the help.
Edit01
Holla again,
I'm doing it as you told me and I have a problem.
This is my code:
// Script here
var data = data2;
var tests = data;
var tests3 = {"457": {"2": {"value": "54.0", "timestamp": 1534246741}, "3": {"value": "22.2", "timestamp": 1534246741}, " 4 ": {" value ":" 260.0 "," timestamp ": 1534246741}," 21 ": {" value ":" 890.0 "," timestamp ": 1534246741}," 1 ": {" value ":" 4.13 "," timestamp ": 1534246741}," 17 ": {" value ":" 194.04687499999997 "," timestamp ": 1534246741}," 5 ": {" value ":" 35.417 "," timestamp ": 1534246741}," 6 ": {" value ":" 26.299999999999997 "," timestamp ": 1534246741}," 8 ": {" value ":" 4.7 "," timestamp ": 1534246741}," 15 ": {" value ":" 0.78 "," timestamp ": 1534246741}," 10 ": {" value ":" 24.94 "," timestamp ": 1534246741}," 22 ": {" value ":" 0.0 "," timestamp ": 1534246741}," 23 ": {" value ":" 0.0 "," timestamp ": 1534246741}," 24 ": {" value ":" 0.0 "," timestamp ": 1534246741}," 26 ": {" value ":" 0.0 "," timestamp ": 1534246741}," 653 ": {" value ":" 0.0 "," timestamp ": 1534246741}," 657 ": {" value ":" - 98.0 "," timestamp ": 1518420299}, "43": {"value": "11.892947103200001", "timestamp": 1534246741}, "42": {"value": "403.61749999999995", "timestamp": 1534246741}}};
var key = Object.keys (data) [0];
var finalobj = {};
for (var and in data [key]) {
    finalobj [e] = {
        type: "float"
        , value: parseFloat (data [key] [e] .value)
        , metadata: {
            timestamp: {
                value: parseInt (data [key] [e] .timestamp)
                , type: "Integer"
            }
        }
    };
}
 var JsonOutput = JSON.stringify (finalobj);
The variable data2 is the one that has my JSON is really the same information that tests3, the code if it works but if I step puts puts data by tests3, which I do not understand since data has the same value and would have to work and the response of JsonOutput is {} but I do it with tests3 if it works correctly.
It looks like it is at the time of retrieving the variable but then I show that it has data and data2 and it is the same information that tests3, I do not understand what happens.
can you help me?
Right now there isn't a built in step for writing nested JSON in Pentaho, you have to use JavaScript to achieve it, there is a really great post here that i'm using as guide to build my own process.

Replace double quotes to single quotes with awk

BEGIN {
q = "\""
FS = OFS = q ", " q
}
{
split($1, arr, ": " q)
for(i in arr ) {
if(arr[i] == "name") {
gsub(q, "'", arr[i+1])
# print arr[1] ": " q arr[2], $2, $3
}
}
}
I have a json file, some data like this:
{"last_modified": {"type": "/type/datetime", "value": "2008-04-01T03:28:50.625462"}, "type": {"key": "/type/author"}, "name": "National Research Council. Committee on the Scientific and Technologic Base of Puerto Rico"s Economy.", "key": "/authors/OL2108538A", "revision": 1}
The name's value have a double quote, I only want to replace the double quote to single quote , not the all double quote, please tell me how to fix it?
awk '{for(i=1;i<=NF;i++) if($i~/name/){ gsub("\042","\047",$(i+1)) }}1' file