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 );
Related
[
{
"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
I am working with a file that has a JSON string in it.
{
"ConfigurationItems": [{
"ActiveDirectory": [{
"Users": [
{ "FirstName": "U1", "LastName": "L1", "Department": "D1", "UserName": "UN1" },
{ "FirstName": "U2", "LastName": "L2", "Department": "D2", "UserName": "UN2" },
{ "FirstName": "U3", "LastName": "L3", "Department": "D3", "UserName": "UN3" },
{ "FirstName": "U4", "LastName": "L4", "Department": "D4", "UserName": "UN4" },
{ "FirstName": "U5", "LastName": "L5", "Department": "D5", "UserName": "UN5" }
]
},
{
"Groups": [
{ "Name": "G1", "Scope": "Global" },
{ "Name": "G2", "Scope": "Global" },
{ "Name": "G3", "Scope": "Global" }
]
},
{
"OU": [
{ "Name": "N1" },
{ "Name": "N2" },
{ "Name": "N3" }
]
}
]
}]
}
I assign this to a variable by doing the following:
$t = Get-content -path $pathtoJSON -raw | ConvertFrom-Json
Now comes the part I cannot explain if I get the count of Users it returns 7
$t.ConfigurationItems.ActiveDirectory.Users.Count
The main issue is when I iterate through this, I will always end up with two $null items in my loop, which throws off my code. How do I ensure I get the correct count from ConvertFrom-Json. For the time being I have worked around this by checking if my array item is not $null
I am using PS 5.1 for this.
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);
Here are the JSON in play:
origJSON
{
"2014": {
"grade": "A-",
"className": "Geography 101"
},
"_id": "564d098e2e59e72412e4d795",
"purl": "3",
"firstName": "Jon",
"finalGrade": "B",
"template": "User.html",
"sensitive": "{"ssn":\"123-00-6789\",\"acc\":\"987654300\",\"password\":\"zxcvbn!\"}"
}
newJSON
{
"purl": "3",
"firstName": "Jon",
"sensitive": {
"password": "qazwx",
"phone": "1234567890"
}
}
merged origJSON
{
"2014": {
"grade": "A-",
"className": "Geography 101"
},
"_id": "564d098e2e59e72412e4d795",
"purl": "3",
"firstName": "Jon",
"finalGrade": "B",
"template": "User.html",
"sensitive": {
"password": "qazwx",
"phone": "1234567890"
}
}
desired origJSON
{
"2014": {
"grade": "A-",
"className": "Geography 101"
},
"_id": "564d098e2e59e72412e4d795",
"purl": "3",
"firstName": "Jon",
"finalGrade": "B",
"template": "User.html",
"sensitive": {
"ssn": "123-00-6789",
"acc": "987654300",
"password": "qazwx",
"phone": "1234567890"
}
}
Here is the code to I've got:
for(var key in newJSON) origJSON[key]=newJSON[key];
Any help to get the nested objects updated instead of replaced ?
Use the lodash library's _.merge() method to achieve the desired result. Here are various examples of the available methods but the _.merge() is the one you are after:
var origJSON = {
"2014": {
"grade": "A-",
"className": "Geography 101"
},
"_id": "564d098e2e59e72412e4d795",
"purl": "3",
"firstName": "Jon",
"finalGrade": "B",
"template": "User.html",
"sensitive": {"ssn":"123-00-6789","acc":"987654300","password":"zxcvbn!"}
};
var newJSON = {
"purl": "3",
"firstName": "Jon",
"sensitive": {
"password": "qazwx",
"phone": "1234567890"
}
};
Using _.assign():
var assigned = _.clone(newJSON);
_.assign(assigned, origJSON);
console.log("assign:", JSON.stringify(assigned, null, 4));
Output
assign: {
"2014": {
"grade": "A-",
"className": "Geography 101"
},
"purl": "3",
"firstName": "Jon",
"sensitive": {
"ssn": "123-00-6789",
"acc": "987654300",
"password": "zxcvbn!"
},
"_id": "564d098e2e59e72412e4d795",
"finalGrade": "B",
"template": "User.html"
}
Using _.merge()
var merged = _.clone(newJSON);
_.merge(merged, origJSON);
console.log("merge:", JSON.stringify(merged, null, 4));
Output
merge: {
"2014": {
"grade": "A-",
"className": "Geography 101"
},
"purl": "3",
"firstName": "Jon",
"sensitive": {
"password": "zxcvbn!",
"phone": "1234567890",
"ssn": "123-00-6789",
"acc": "987654300"
},
"_id": "564d098e2e59e72412e4d795",
"finalGrade": "B",
"template": "User.html"
}
Using _.defaults()
var defaulted = _.clone(newJSON);
_.defaults(defaulted, origJSON);
console.log("defaults:", JSON.stringify(defaulted, null, 4));
Output
defaults: {
"2014": {
"grade": "A-",
"className": "Geography 101"
},
"purl": "3",
"firstName": "Jon",
"sensitive": {
"password": "zxcvbn!",
"phone": "1234567890",
"ssn": "123-00-6789",
"acc": "987654300"
},
"_id": "564d098e2e59e72412e4d795",
"finalGrade": "B",
"template": "User.html"
}
Check the demo below.
var origJSON = {
"2014": {
"grade": "A-",
"className": "Geography 101"
},
"_id": "564d098e2e59e72412e4d795",
"purl": "3",
"firstName": "Jon",
"finalGrade": "B",
"template": "User.html",
"sensitive": {"ssn":"123-00-6789","acc":"987654300","password":"zxcvbn!"}
};
var newJSON = {
"purl": "3",
"firstName": "Jon",
"sensitive": {
"password": "qazwx",
"phone": "1234567890"
}
};
var assigned = _.clone(newJSON);
_.assign(assigned, origJSON);
console.log("assign:", assigned);
var merged = _.clone(newJSON);
_.merge(merged, origJSON);
console.log("merge:", merged);
var defaulted = _.clone(newJSON);
_.defaults(defaulted, origJSON);
console.log("defaults:", defaulted);
pre.innerHTML = "assign: " + JSON.stringify(assigned, null, 4) + "</br>merge: " + JSON.stringify(merged, null, 4) + "</br>defaults: "+ JSON.stringify(defaulted, null, 4);
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js"></script>
<pre id="pre"></pre>
There are plenty of options to do this with utility libraries like lodash and also ES6 destructuring (maybe).
But, imo, the most maintainable way and while using one of those, would be using an object factory. Assuming that the object structure will be the same all over... It's easier to wrap you head around, than libraries you don't know or complex clone/merge/dedupe algorithms. My bet is also that JS engines optimize factories better.
function outputFactory(input_1, input_2){
// some conditions and/ or data handling here
var desiredProperty = (input_1.someProperty >= input_2.someProperty) ? input_1.someProperty : input_2.someProperty;
return {
desiredProperty: desiredProperty
}
}
var merged = outputFactory({someProperty:1}, {someProperty:2})
console.log(merged)
Hope this gives you an option.
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"