I have a problem with the construction of a Json for Amazon Kinesis.
This json has to have this format:
{
"Records": [
{
"Data": "XzxkYXRhPl8x",
"PartitionKey": "partitionKey1"
},
{
"Data": "f1PxFQo92Afh",
"PartitionKey": "partitionKey2"
},
{
"Data": "Gi4sEdd08HypA",
"PartitionKey": "partitionKey3"
}
],
"StreamName": "exampleStreamName"
}
I use an BeanShell Sampler to create the json as a buffer:
import org.json.JSONArray;
import org.json.JSONObject;
//Dichiarazione variabili
int timestampValue=(${startTime}+${i}+1);
float current_powerValue=${current_power_1}+${__Random(0,10)};
String idValue=${__threadNum}+"_"+"5";
JSONObject part = new JSONObject();
//Create JSON
part.put("timestamp",timestampValue);
part.put("parent","${__threadNum}");
part.put("id",idValue);
part.put("state","on");
part.put("today_kwh",65);
part.put("current_power",current_powerValue);
part.put("today_on_time",0);
part.put("on_for",0);
part.put("today_standby_time",0);
//ADD json to array
if(${i}%(${bufferSize}*${sample}-1)==0 && ${i}!=0 || ${i}==${totalNumber}-${endOfDb}){
//Add to json variable the last json created
vars.put("json",vars.get("json")+part.toString());
//Make an JSONObject by json variable of jmeter
JSONObject tempArray= new JSONObject(vars.get("json"));
log.info(tempArray.toString());
//Add tempArray into JSONArray so that it adds square brackets
JSONArray records= new JSONArray();
records.put(tempArray);
//Add the second field streamName
JSONObject kinesis = new JSONObject();
kinesis.put("records",records);
kinesis.put("streamName","kinesis");
//save into jsonBuffer
vars.put("jsonBuffer",kinesis.toString());
//restart json variable
vars.put("json","");
}
else{
//add new json into variable so to store it.
vars.put("json", vars.get("json")+part.toString()+",");
}
I use json variable in jmeter to save the json for each iteraction and when the "i" variable respect the if clause then I start to create the json structure.
So I add the last json to the jmeter variable, then I create a JSONObject to store this json but when i do this it store only one json (because it is an object).
Unfortunately if I store in a JSONArray it add "" because read the variable json as a string.
The best solution would be use only JSONObject and JSONArray but how I use the same object for all the iteractions(in jmeter i can't use JSONArray)
This is my jmx
You could trt with this snippet:
if(${i}%(${bufferSize}*${sample}-1)==0 && ${i}!=0 || ${i}==${totalNumber}-${endOfDb}){
vars.put("json",vars.get("json")+part.toString());
JSONArray records= new JSONArray("["+vars.get("json")+"]");
log.info(records.toString());
//records.put(tempArray);
JSONObject kinesis = new JSONObject();
kinesis.put("records",records);
kinesis.put("streamName","kinesis");
vars.put("jsonBuffer",kinesis.toString());
vars.put("json","");
}
Related
How can I iterate a json object and get it's values in dart.
Below is an Example json object that is want to get it's value.
"SomeJsonList": {
"image": ["imageUrl1", "imageUrl2"],
"video": ["videoUrl1", "videoUrl2"]
}
To get the values of the image and video object as a list.
The output i am looking for:
var urlList = [];
urlList = [imageUrl1, imageUrl2,videoUrl1,
videoUrl2]
You can merge list items using addAll as below
void main() {
Map<String, List<String>> json = {
"image": ["imageUrl1", "imageUrl2"],
"video": ["videoUrl1", "videoUrl2"]
};
List<String>? img = json["image"];
List<String>? vid = json["video"];
List<String> list = [];
list.addAll(img!); // better check if `img` not null instead of "!"
list.addAll(vid!); // better check if `vid` not null instead of "!"
// list.addAll(xyz); // some more if needed
print(list); // [imageUrl1, imageUrl2, videoUrl1, videoUrl2]
}
I have a field type JsonObject in Baqend. How can I save data with it's SDK ?
you have to set the field to your JsonObject.
For example:
let obj = new DB.Test();
obj.json = { "foo": "bar" };
obj.save();
I have a query, I have to read key "class" under key"studentData" from the below mentioned json response.
{
"studentData": [
{
"name": "john",
"class": 2,
"rollno": "2015"
}
],
"yearofenrollment": 2017
}
Please help.
Thanks in advance
JSONObject jsonObjectResponse = new JSONObject(response.toString()); //obtain the object
JSONArray jsonMainNode = jsonObjectResponse.optJSONArray("studentData");//get array from object
JSONObject jsonChildNode = jsonMainNode.getJSONObject(0);//get first object in array
String studentDataValue = jsonChildNode.optString("class");//obtain value from class key
I want to parse a Json File where all Json Arrays have the same Name:
[
{
"mobileMachine":{
"condition":"GOOD",
"document":"a",
"idNr":"ce4f5a276a55023efced9c6a4b02bf4fcff04c06b4338467c8679770bff32313f7f372b5ec2f7527dad0de47d0fb117e"
}
},
{
"mobileMachine":{
"condition":"GOOD",
"document":"b",
"idNr":"ce4f5a276a8e023efced9c6a4b02bf4fcff04c06b4338467c8679770bff32313f7f372b5ec2f7527dad0de47d0fb217e"
}
},
...
]
So here is my little Code:
JSONArray json = new JSONArray(urlwhereIGetTheJson);
for (int count = 0; count < json.length(); count++) {
JSONObject obj = json.getJSONObject(count);
String condition = obj.getString("condition");
String document = obj.getString("document");
String idNr = obj.getString("idNr");
db.addMachine(new MachineAdapter(condition, document, idNr));
}
I hope u can show me how to parse the JSON File correctly. Thank you
I can't edit the JSON File. (The File include more than 300 mobilemachines. I have shorten this).
(Sorry for my English)
Edit: You are using the new JSONArray() constructor incorrectly. Have a look at the documentation. You can not directly pass the url there. You have to obtain it first, and then pass the json to the constructor.
The following piece of code does what you want to do:
JSONArray jsonArray = new JSONArray(json);
int numMachines = jsonArray.length();
for(int i=0; i<numMachines; i++){
JSONObject obj = jsonArray.getJSONObject(i);
JSONObject machine = obj.getJSONObject("mobileMachine");
String condition = machine.getString("condition");
String document = machine.getString("document");
String idNr = machine.getString("idNr");
db.addMachine(new MachineAdapter(condition, document, idNr));
}
You forgot to obtain the "mobileMachine" json object, and tried to access condition/document/idNr directly.
If you have control over the XML, you could make it smaller by removing the "mobileMachine" node:
[
{
"condition":"GOOD",
"document":"a",
"idNr":"ce4f5a276a55023efced9c6a4b02bf4fcff04c06b4338467c8679770bff32313f7f372b5ec2f7527dad0de47d0fb117e"
},
{
"condition":"GOOD",
"document":"b",
"idNr":"ce4f5a276a8e023efced9c6a4b02bf4fcff04c06b4338467c8679770bff32313f7f372b5ec2f7527dad0de47d0fb217e"
},
...
]
Change it to
JSONArray json = new JSONArray(jsonString);
for (int count = 0; count < json.length(); count++) {
JSONObject obj = json.getJSONObject(count).getJSONObject("mobileMachine");
String condition = obj.getString("condition");
String document = obj.getString("document");
String idNr = obj.getString("idNr");
db.addMachine(new MachineAdapter(condition, document, idNr));
}
You forgot "mobileMachine".
I have a json response coming in from a php file, the json response looks as below results:
[
{
"header": "Client ID",
"dataindex": "clientID"
},
{
"header": "Client Name",
"dataindex": "clientName"
},
{
"header": "Progress Bar",
"dataindex": "progressBar"
}
]
Now I want to copy this data into an array in the following way
var allColumns = [];
//loop through the json response
var singleColumn = [];
singleColumn['header'] = Client ID from the json response whose key is header
singleColumn[dataindex'] = clientID from the json response whose key is header.
Please note: I need to do this is extjs3.
If I am getting you correctly you are getting the JSON as a string from an ajax call to your PHP handler in a string format and you want to cast it into a Javascript array of coloumn objects.
you can do it like this:
var allColumns = Ext.decode(YOUR PHP JSON FORMATTED RESULT);
this will allow you to iterate over the result set as an in memory javascript array of objects like this:
for(var i=0 ; i < allColumns.length; i++){
allColumns[i].header ...
allColumns[i].dataindex ...
}
I hope I got you right...