Reading a json file and displaying data in ejs - json

New to programming...probably a stupid question...
I have a users.json file.
{
"User": {
"userID": [
{
"$": {
"id": "3"
},
"username": [
"patient1"
],
"email": [
"x#gmail.com"
],
"Role_IDrole": [
"1"
],
"Organization": [
"1"
],
"Lat": [
"59.6567"
],
"Long": [
"16.6709"
]
}
]
}
}
I am requiring it in my routes
var userData = require('../users.json');
My route
app.get('/doctor', isLoggedIn, function(req, res) {
res.render('doctortest.ejs', {
user : req.user,
data: userData
});
});
In my views/doctortest.ejs, I simply want to print the "email"
<%data.User.userID.email%>
It doesn't show anything. What's going on?

Related

JSON Object complex example get localId in users object

{
"kind": "identitytoolkit#GetAccountInfoResponse",
"users": [
{
"localId": "XR9iEJd8SpWLZyIlTclB77lquz52",
"email": "abdulrehmansnp57#gmail.com",
"passwordHash": "UkVEQUNURUQ=",
"emailVerified": false,
"passwordUpdatedAt": 1666284051842,
"providerUserInfo": [
{
"providerId": "password",
"federatedId": "abdulrehmansnp57#gmail.com",
"email": "abdulrehmansnp57#gmail.com",
"rawId": "abdulrehmansnp57#gmail.com"
}
],
"validSince": "1666284051",
"lastLoginAt": "1666346094257",
"createdAt": "1666284051842",
"lastRefreshAt": "2022-10-21T09:54:54.257Z"
}
]
}
If I understood your question correctly, you want to return the "localId", but since it's an array I will assume you will have multiple users. So here's an example of how you can return an array of the "localId" in react or JavaScript in general:
const obj= { "kind": "identitytoolkit#GetAccountInfoResponse", "users": [ { "localId": "XR9iEJd8SpWLZyIlTclB77lquz52", "email": "abdulrehmansnp57#gmail.com", "passwordHash": "UkVEQUNURUQ=", "emailVerified": false, "passwordUpdatedAt": 1666284051842, "providerUserInfo": [ { "providerId": "password", "federatedId": "abdulrehmansnp57#gmail.com", "email": "abdulrehmansnp57#gmail.com", "rawId": "abdulrehmansnp57#gmail.com" } ], "validSince": "1666284051", "lastLoginAt": "1666346094257", "createdAt": "1666284051842", "lastRefreshAt": "2022-10-21T09:54:54.257Z" } ] };
const ids= obj.users.map((e)=>e.localId);
response: ids=[XR9iEJd8SpWLZyIlTclB77lquz52];

Mapping complex Observable json to class or interface

I have an API I am consuming with a service in Angular 8.0.0. I cannot seem to understand how to map the json returned to an object.
All of the examples I have seen have been simple examples of an array of clearly defined and easily traversed json. The json set I am getting back is quite complex.
Ideally, since there is a lot of repeated patterns in there, I would make a class with other classes as members of the class.
Is there a way to just transform the returned observable json into the associated dictionary of dictionaries?
If I were to template this, I'm struggling with the fact that I get a dictionary back and not an array.
Here is the service design pattern that I am following. Since this design pattern transforms a simple json payload into a simple interface, it's not complex enough for me to try and transpose to my use-case. I have doubt on the ability to transform this complex dataset to an interface or class.
getTeams() {
return this.http.get<ITeam[]>(this._url).pipe(
retry(1), catchError(error => {
return throwError(error.message || 'Undefined Server Error');
}));
}
Here is my sample dataset similar to what I am trying to consume in my angular component via my service:
{
"stlouis_teams": {
"blues": {
"version": "12.0-57.24",
"image": "stl_team_photo.jpg",
"image_md5": "fffffc429735ffff4d6c716c113f3ffff",
"stadium_phones": {
"landlines": [
"210-555-1212",
"309-555-1212"
],
"cells": [
"312-555-1212",
"502-555-1212"
]
},
"coaching_staff": {
"coach": {
"image": "bill_brasky.jpg",
"image_locations": {
"http": [
"http://example.com/images/bill_brasky.jpg",
"http://example.com/bill_brasky.jpg"
],
"ftp": [
"ftp://example.com/public/images/bill_brasky.jpg",
"ftps://example.com/secure/aoe124/public/images/bill_brasky.jpg"
]
},
"image_md5": "4444444d0a2c1cad1437570f0e4444444"
},
"asst_coach": {
"image": "bill_billy.jpg",
"image_locations": {
"http": [
"http://example.com/images/bill_billy.jpg",
"http://example.com/bill_billy.jpg"
],
"ftp": [
"ftp://example.com/public/images/bill_billy.jpg",
"ftps://example.com/secure/aoe124/public/images/bill_billy.jpg"
]
},
"image_md5": "333333333d0a2c1cad1437570f0e33333333"
}
}
},
"jv_blues": {
"version": "1.0-0.0",
"image": "jv_stl_team_photo.jpg",
"image_md5": "9999c42973599994d6c716c113f39999",
"stadium_phones": {
"landlines": [
"210-555-1111",
"309-555-1111"
],
"cells": [
"312-555-1111",
"502-555-1111"
]
},
"coaching_staff": {
"coach": {
"image": "bill_brasky.jpg",
"image_locations": {
"http": [
"http://example.com/images/bill_brasky.jpg",
"http://example.com/bill_brasky.jpg"
],
"ftp": [
"ftp://example.com/public/images/bill_brasky.jpg",
"ftps://example.com/secure/aoe124/public/images/bill_brasky.jpg"
]
},
"image_md5": "4444444d0a2c1cad1437570f0e4444444"
},
"asst_coach": {
"image": "bill_billy.jpg",
"image_locations": {
"http": [
"http://example.com/images/bill_billy.jpg",
"http://example.com/bill_billy.jpg"
],
"ftp": [
"ftp://example.com/public/images/bill_billy.jpg",
"ftps://example.com/secure/aoe124/public/images/bill_billy.jpg"
]
},
"image_md5": "333333333d0a2c1cad1437570f0e33333333"
}
}
}
},
"chicago_teams": {
"blackhawks": {
"version": "11.33",
"image": "chi_team_photo.jpg",
"image_md5": "00000c42973500004d6c716c113f30000",
"stadium_phones": {
"landlines": [
"702-555-1212",
"412-555-1212"
],
"cells": [
"217-555-1212",
"904-555-1212"
]
},
"coaching_staff": {
"coach": {
"image": "bill_brasky.jpg",
"image_locations": {
"http": [
"http://example.com/images/bill_brasky.jpg",
"http://example.com/bill_brasky.jpg"
],
"ftp": [
"ftp://example.com/public/images/bill_brasky.jpg",
"ftps://example.com/secure/aoe124/public/images/bill_brasky.jpg"
]
},
"image_md5": "4444444d0a2c1cad1437570f0e4444444"
},
"asst_coach": {
"image": "bill_billy.jpg",
"image_locations": {
"http": [
"http://example.com/images/bill_billy.jpg",
"http://example.com/bill_billy.jpg"
],
"ftp": [
"ftp://example.com/public/images/bill_billy.jpg",
"ftps://example.com/secure/aoe124/public/images/bill_billy.jpg"
]
},
"image_md5": "333333333d0a2c1cad1437570f0e33333333"
}
}
},
"minor_blackhawks": {
"version": "1002.2",
"image": "minor_chi_team_photo.jpg",
"image_md5": "ccccccc42973500004d6c716c113fccccc",
"stadium_phones": {
"landlines": [
"702-555-2222",
"412-555-2222"
],
"cells": [
"217-555-2222",
"904-555-2222"
]
},
"coaching_staff": {
"coach": {
"image": "bill_brasky.jpg",
"image_locations": {
"http": [
"http://example.com/images/bill_brasky.jpg",
"http://example.com/bill_brasky.jpg"
],
"ftp": [
"ftp://example.com/public/images/bill_brasky.jpg",
"ftps://example.com/secure/aoe124/public/images/bill_brasky.jpg"
]
},
"image_md5": "4444444d0a2c1cad1437570f0e4444444"
},
"asst_coach": {
"image": "bill_billy.jpg",
"image_locations": {
"http": [
"http://example.com/images/bill_billy.jpg",
"http://example.com/bill_billy.jpg"
],
"ftp": [
"ftp://example.com/public/images/bill_billy.jpg",
"ftps://example.com/secure/aoe124/public/images/bill_billy.jpg"
]
},
"image_md5": "333333333d0a2c1cad1437570f0e33333333"
}
}
}
}
}

Angular HTTP Res JSON Data print in html

Below is the HTTP get request response. how can i print same in view using *ngFor . iam trying to print single object print like data[0].value1 in console. it returns undefined. inside data array 3 JSON Objects. and some arrays also
Any help much appreciated...
getEmp(){
this.EmpService.getAccounts().subscribe((res : any[])=>{
this.products = JSON.stringify(res);
});
.
{
"data": [
{
"value1": "3546786908765432",
"category": "Clothing (Brand)",
"category_list": [
{
"id": "001",
"name": "Clothing (Brand)"
},
{
"id": "39324324230",
"name": "Boutique Store"
},
{
"id": "12342324324",
"name": "Website"
}
],
"name": "wqeqws",
"id": "qwsqwsq w",
"tasks": [
"ANALYZE",
"ADVERTISE",
"MODERATE",
"CREATE_CONTENT",
"MANAGE"
]
},
{
"value1": "111-9=-09876543",
"category": "Education",
"category_list": [
{
"id": "23456",
"name": "Education"
}
],
"name": "sdcsfcsdfvsd",
"id": "111111111111",
"tasks": [
"ANALYZE",
"ADVERTISE",
"MODERATE",
"CREATE_CONTENT",
"MANAGE"
]
}
],
"paging": {
"cursors": {
"before": "dsfsds",
"after": "sdfsfs"
}
}
}
this.products = res.data;
In your page
< *ngFor="let item of products >

Modify JSON after getting a response from sequelize

I get a JSON from my sequelize function. This i have to modify, because i have to send it to a database importer, who needs it in a fixed form.
Is there a way to customize this function so that I get back the desired result ?
models:
User.associate = function (models) {
User.hasMany(models.SurveyResult)
}`
SurveyResult.associate = function (models) {
SurveyResult.belongsTo(models.User)
The function:
async mediImport (req, res) {
try {
const transaction = await User.findAll({
where: { released: true },
// Select forename as Vorname, name as Nachname
attributes: [
['forename', 'PAPPS286'],
['name', 'Nachname'],
['birthdate', 'PADPS60']
],
include: [{ model: SurveyResult, attributes: ['result'] }]
}).map(user => user.toJSON())
res.send({
transaction
}
)
}
this is the JSON which i get from the function:
{
"transaction": [
{
"PAPPS286": "Tes",
"Nachname": "Josef",
"PADPS60": null,
"SurveyResults": [ {
"result": {
"name": "blau",
"email": "mail",
"birthdate": "01.02.1990"
}
}]
},
{
"PAPPS286": "Dampf",
"Nachname": "Hans",
"PADPS60": null,
"SurveyResults": [
{
"result": {
"name": "blau",
"email": "mail",
"birthdate": "01.02.1990"
}
}
]
},
]
},
This is the JSON form i need:
{
"transaction": [
PAD{
"PAPPS286": "Tes",
"Nachname": "Josef",
"PADPS60": null,
"MH": {
"name": "blau",
"email": "mail",
"birthdate": "01.02.1990"
},
PAD{
"PAPPS286": "Dampf",
"Nachname": "Hans",
"PADPS60": null,
"MH": {
"name": "blau",
"email": "mail",
"birthdate": "01.02.1990"
}
},
]
},
Perhaps there is a way to modify the JSON after i get it back. But i dont Knoe how this can be work.
The below map does what I think you are looking for. You may need to modify it, especially around accessing the variables you need, based on the key. (t.PAPPS286 and t.PADPS60)
Additionally, I am only grabbing the first survey result SurveyResults. Unsure what you want if there are none or if there are more than 1.
var obj = {
"transaction": [{
"PAPPS286": "Tes",
"Nachname": "Josef",
"PADPS60": null,
"SurveyResults": [{
"result": {
"name": "blau",
"email": "mail",
"birthdate": "01.02.1990"
}
}]
},
{
"PAPPS286": "Dampf",
"Nachname": "Hans",
"PADPS60": null,
"SurveyResults": [{
"result": {
"name": "blau",
"email": "mail",
"birthdate": "01.02.1990"
}
}]
},
]
}
obj.transaction = obj.transaction.map((t) => {
return Object.assign({
"PAPPS286": t.PAPPS286,
"Nachname": t.Nachname,
"PADPS60": t.PADPS60,
"MH": {
"name": t.SurveyResults[0].result.name,
"email": t.SurveyResults[0].result.email,
"birthdate": t.SurveyResults[0].result.birthdate
}
})
})
console.log(obj);

How to check if a key exists in a nested JSON object in node?

I've got the following JSON being sent to the server from the browser:
{
"title": "Testing again 2",
"abstract": "An example document",
"_href": "http://google.com",
"tags": [ "person" ],
"attributes": [ {
"id": 1,
"type": "TEXT",
"data": "test"
} ],
"sections": [ {
"id": 1,
"type": "LIST",
"data": [ {
"revision": 124,
"text": "test"
} ]
} ]
}
I need to make sure that the keys "_href", "id" and "revision" are not in the object anyplace at any level.
I found this but it doesn't quite work.
I searched npms.io and found has-any-deep which you can use after JSON.parse ing the JSON.
you need to parse json then check into the data
var str = '{
"title": "Testing again 2",
"abstract": "An example document",
"_href": "http://google.com",
"tags": [ "person" ],
"attributes": [ {
"id": 1,
"type": "TEXT",
"data": "test"
} ],
"sections": [ {
"id": 1,
"type": "LIST",
"data": [ {
"revision": 124,
"text": "test"
} ]
} ]
}';
var jsonObj = JSON.parse(str);
if ( typeof jsonObj._href == 'undefined') {
// check
}
A simple but not 100% foolproof solution would be to parse the JSON to string, and just search for your keys:
var a = JSON.stringify(JSONObject);
var occurs = false;
['"_href"', '"id"', '"version"'].forEach(function(string) {
if(a.indexOf(string) > -1) occurs = true;
});
The issue of course, is if there are values that match
'_href', 'id', 'version' in your JSON. But if you want to use native JS, I guess this is a good bet.
var a = {
"title": "Testing again 2",
"abstract": "An example document",
"tags": [ "person" ],
"attributes": [ {
"type": "TEXT",
"data": "test"
} ],
"sections": [ {
"type": "_href asdad",
"data": [ {
"text": "test"
} ]
} ]
},
b = {
"title": "Testing again 2",
"abstract": "An example document",
"_href": "http://google.com",
"tags": [ "person" ],
"attributes": [ {
"id": 1,
"type": "TEXT",
"data": "test"
} ],
"sections": [ {
"id": 1,
"type": "LIST",
"data": [ {
"revision": 124,
"text": "test"
} ]
} ]
},
aJson = JSON.stringify(a),
bJson = JSON.stringify(b);
var occursa = false, occursb = false;
['"_href"', '"id"', '"version"'].forEach(function(string) {
if(aJson.indexOf(string) > -1) { occursa = true};
});
['"_href"', '"id"', '"version"'].forEach(function(string) {
if(bJson.indexOf(string) > -1) { occursb = true};
});
console.log("a");
console.log(occursa);
console.log("b");
console.log(occursb);
You could use the optional second reviver parameter to JSON.parse for this:
function hasBadProp(json) {
let badProp = false;
JSON.parse(json, (k, v) => {
if ([_href", "id", "revision"].includes(k)) badProp = true;
return v;
});
return badProp;
}