I have json data and I want to convert it into object format for doing a create operation.
json
[
{
"user": {
"id": 83,
"username": "das",
"first_name": "dsafha",
"last_name": "dfksdfk",
"email": "sasda@gmail.com",
"is_active": true,
"is_superuser": false
},
"role": "testBu"
},
{
"user": {
"id": 84,
"username": "sadfds",
"first_name": "dshhgds",
"last_name": "fsdjsl",
"email": "fdjgd@gmail.com",
"is_active": true,
"is_superuser": false
},
"role": "testeditrole"
},
{
"user": {
"id": 86,
"username": "fs",
"first_name": "efhks",
"last_name": "sofdh",
"email": "fdshk@gmail.com",
"is_active": true,
"is_superuser": false
},
"role": "testeditrole"
},
{
"user": {
"id": 87,
"username": "xz",
"first_name": "vj",
"last_name": "vkfd",
"email": "sdsl@gmail.com",
"is_active": true,
"is_superuser": false
},
"role": "testeditrole"
}
]
I tried doing it like this
componet.ts
let user:any = {};
user["username"] = this.user.user.email
user["first_name"] = this.user.user.first_name
user["last_name"]= this.user.user.last_name
user["email"]= this.user.user.email
this.userData["user"] = user
this.userData["role"] = this.user.role
and while consoling after entering the data into the input fields,I'm not getting the role data. I do get the other data. The role data appears empty. I do think its because of how i wrote the code in componet.ts that's causing the issue.
You can always create models:
export class User {
id: string,
username: string,
first_name: string,
last_name: string,
email: string,
is_active: boolean,
is_superuser: boolean
}
export class UserDetails{
user:User;
role:string;
}
Then:
// Assume you have received the json in string form in
'resultlist' variable
let dataList = <Array<UserDetails>>JSON.parse(resultlist);
Or:
// Assume you have received the json in object form in 'resultlist' variable
let dataList = <Array<UserDetails>>resultlist;