How to concat string to an existing array element using lodash - json

I have a json object which has the following form
"data": [{
"firstName": "XYZ",
"lastName": "Admin",
"userId": 1,
"companyLogo":"Logo"
}, {
"firstName": "ABC",
"lastName": "Admin",
"userId": 1,
"companyLogo":"Logo1"
}, {
"firstName": "EFG",
"lastName": "Admin",
"userId": 1,
"companyLogo":"Logo2"
}]
I want the following output to concat the logo name with its path using lodash in nodejs
"data": [{
"firstName": "XYZ",
"lastName": "Admin",
"userId": 1,
"companyLogo":"E:/Logo"
}, {
"firstName": "ABC",
"lastName": "Admin",
"userId": 1,
"companyLogo":"E:/Logo1"
}, {
"firstName": "EFG",
"lastName": "Admin",
"userId": 1,
"companyLogo":"E:/Logo2"
}]
Is there any function existing which supports this type of concatenation?

There's no need for an external library:
var data = [
{
"firstName": "XYZ",
"lastName": "Admin",
"userId": 1,
"companyLogo":"Logo"
},
{
"firstName": "ABC",
"lastName": "Admin",
"userId": 1,
"companyLogo":"Logo1"
},
{
"firstName": "EFG",
"lastName": "Admin",
"userId": 1,
"companyLogo":"Logo2"
}
]
var new_data = data.map(function(elem){ elem.companyLogo = "E: /" + elem.companyLogo; return elem });
console.log(new_data);

Related

Can i covert this in a CSV format where all the element gonna be under one row, without repeating the doc attribute?

[
{
"doc": "ghgagsa",
"element": [
{
"firstName": "John",
"lastName": "Doe"
},
{
"firstName": "Anna",
"lastName": "Smith"
},
{
"firstName": "Peter",
"lastName": "Jones"
}
]
},
{
"doc": "ghgagsaaa",
"element": [
{
"firstName": "Joaahn",
"lastName": "Doae"
},
{
"firstName": "Anana",
"lastName": "Smiath"
},
{
"firstName": "Petaaer",
"lastName": "Jonaes"
}
]
}
]
Using jq, it's .[].element[]|[.firstName,.lastName]|#csv
Here an example https://jqplay.org/s/XmlRS6Yh9v

Json error of undefine when using for loop

im learning JSON and encounter a problem when looping JSON.
This is my JSON response
{
"success": 1,
"category": [{
"id": "1",
"category": "Editorial",
"created_at": "2019-11-05 18:10:31",
"firstname": "abc",
"lastname": "xyz"
}, {
"id": "2",
"category": "Sports",
"created_at": "2019-11-05 19:25:50",
"firstname": "abc",
"lastname": "xyz"
}, {
"id": "3",
"category": "Health",
"created_at": "2019-11-05 19:27:23",
"firstname": "abc",
"lastname": "xyz"
}, {
"id": "4",
"category": "Food",
"created_at": "2019-11-05 19:39:17",
"firstname": "abc",
"lastname": "xyz"
}]}
and this is my loop
for(var i = 0; i <= jsonData.category.length; i++){
console.log(jsonData.category[i]['firstname']);}
it does print in console but it gives me this error
Cannot read property 'firstname' of undefined
at Object.success
Use < instead of <=. Otherwise you reach an undefined index.
for(var i = 0; i < jsonData.category.length; i++){
console.log(jsonData.category[i]['firstname']);
}

angularJS JSON parsing

I am new to angularJS and trying to parse the data and display it on a page.
{
"count": 13,
"status": 200,
"statusMessage": "OK",
"userContact": [
{
"user": {
"id": 1,
"firstName": "test",
"lastName": "test",
"universityId": 1,
"email": "test#harbingergroup.com",
"password": "",
"phoneNo": "1234567890",
"gender": "M",
"userType": 1,
"medicalComments": "",
"dob": "2015-02-22",
"universityAffiliation": 1,
"cityId": "1"
},
"userContactList": {
"count": 2,
"status": 200,
"statusMessage": "OK",
"contact": [
{
"id": 2,
"userId": 1,
"firstName": "test",
"lastName": "user",
"phoneNo": "9876543210",
"email": "test#example.com"
},
{
"id": 24,
"userId": 1,
"firstName": "first1",
"lastName": "last1",
"phoneNo": "9876543210",
"email": "test#example.com"
}
]
}
}]
}
this is my JSON data.What i am trying is :
<tr ng-repeat="studentDetails in studentProfileData">
studentDetails.status for getting status and studentDetails.userContacts.firstName for first name
but I am not getting the data. What is wrong here?
Assuming that studentProfileData contains a list of users (userContact):
studentDetails.user.firstName
will contain "test"

Jmeter JSON Path Assertion

Below is my JSON response data, I need to do assertion using the below response. I tried in many ways to write JSON path & Expected Value. Always assertion fails. What I want is please help to write the path and expected value for the below data
{
"statusCode": 200,
"statusMessage": "Success",
"errorMessage": "",
"responseData": {
"id": 15,
"userName": "partner#tarento.com",
"firstName": "tarento",
"lastName": "test1",
"phoneNumber": "1234567812",
"email": "partner#tarento.com",
"password": "",
"city": "",
"agentList": [
{
"id": 37,
"userName": "Rahul.antonyRaj#tarento.com",
"firstName": "Sanjay",
"lastName": "rahul",
"phoneNumber": "7411269480",
"email": "Rahul.antonyRaj#tarento.com",
"password": "",
"active": true
},
{
"id": 68,
"userName": "jinesh.sumedhan#tareto.com",
"firstName": "jinesh",
"lastName": "sumedhan",
"phoneNumber": "9400993826",
"email": "jinesh.sumedhan#tareto.com",
"password": "",
"active": true
},
{
"id": 108,
"userName": "a.sanjayrahul#gmail.com",
"firstName": "Rahul",
"lastName": "Antony",
"phoneNumber": "9994590241",
"email": "a.sanjayrahul#gmail.com",
"password": "",
"active": true
},
{
"id": 304,
"userName": "a.sanjayrajish#gmail.com",
"firstName": "Agent",
"lastName": "Agent",
"phoneNumber": "9025699716",
"email": "a.sanjayrajish#gmail.com",
"password": "",
"active": true
}
],
"roleName": "admin",
"sessionKey": "435tnerLt9813942160478oDse46345635#1",
"partner": {
"id": 1,
"name": "Tarento",
"cityList": [
"bangalore",
"mumbai"
],
"phone": "1234567812",
"url": ""
},
"isActive": true,
"isDeleted": false,
"roleId": 1,
"countryCode": "",
"tags": [
{
"tagId": 1,
"name": "all",
"description": "this is default tag of all driver."
},
{
"tagId": 2,
"name": "airport",
"description": ""
},
{
"tagId": 3,
"name": "street",
"description": "any text message"
},
{
"tagId": 255,
"name": "night",
"description": "night"
}
]
}
}
I received the following response
For start following JSONPath Assertion will test your statusCode
$.statusCode
put 200 to Expected Value of JSONPath Assertion.
This one is for userName
$.responseData.userName
Easy, isn't it? See Parsing JSON guide for more useful examples and how-tos.
I found the JSR223 Assertion with script language javascript to be the easiest. at least if you have knowledge in java and javascript. And no need to add any plugins.
My working code in detail:
var json = JSON.parse(SampleResult.getResponseDataAsString());
if (json.statusCode != 200) {
AssertionResult.setFailureMessage(""
+ json.statusCode
+ " " + json.statusMessage
+ " " + json.errorMessage);
AssertionResult.setFailure(true);
}
I personally prefer to use BSF PostProcessor in coupling with Groovy language. Example of how to parse JSON with Groovy you can find here how to parse json using groovy

How can i change the Json Data Format?

i have the following Json Data with following format...
var oData = [
{
"0":
{
"firstname": "aaa",
"lastname": "zzz",
"Email": "aaa#test.com"
},
"1":
{
"firstname": "bbb",
"lastname": "yyy",
"Email": "bbb#test.com"
},
"2":
{
"firstname": "ccc",
"lastname": "www",
"Email": "ccc#test.com"
}
}];
Can we convert to the following format ??
var rData = [
{
"firstname": "aaa",
"lastname": "zzz",
"Email": "aaa#test.com"
},
{
"firstname": "bbb",
"lastname": "yyy",
"Email": "bbb#test.com"
},
{
"firstname": "ccc",
"lastname": "www",
"Email": "ccc#test.com"
}];
var oData = [{
"0":
{
"firstname": "aaa",
"lastname": "zzz",
"Email": "aaa#test.com"
},
"1":
{
"firstname": "bbb",
"lastname": "yyy",
"Email": "bbb#test.com"
},
"2":
{
"firstname": "ccc",
"lastname": "www",
"Email": "ccc#test.com"
}
}],
data = oData[0],
rData = [];
JSON.stringify( data, function( key, value ) {
rData.push( value );
});
console.log( rData );