Jijo Robin
Jijo Robin

Reputation: 385

convert json data to object in component.ts

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": "[email protected]",
        "is_active": true,
        "is_superuser": false
    },
    "role": "testBu"
},
{
    "user": {
        "id": 84,
        "username": "sadfds",
        "first_name": "dshhgds",
        "last_name": "fsdjsl",
        "email": "[email protected]",
        "is_active": true,
        "is_superuser": false
    },
    "role": "testeditrole"
},
{
    "user": {
        "id": 86,
        "username": "fs",
        "first_name": "efhks",
        "last_name": "sofdh",
        "email": "[email protected]",
        "is_active": true,
        "is_superuser": false
    },
    "role": "testeditrole"
},
{
    "user": {
        "id": 87,
        "username": "xz",
        "first_name": "vj",
        "last_name": "vkfd",
        "email": "[email protected]",
        "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.

Upvotes: 0

Views: 1197

Answers (4)

VizardCrawler
VizardCrawler

Reputation: 1303

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;

Upvotes: 6

H Mirindra
H Mirindra

Reputation: 94

I think the best way is, create an Object corresponding with the JSON's structure and you assign just the data JSON to the Array of the object.

class User{
   id:string;
   username:string;
   firstName:string;
   lastName:string;
   email:string;
   isActive:boolean;
   isSuperviser:boolean;
}
class JSONData{
   user:User;
   role:string
}
 data:JSONData[] = yourJson.data;

You adjust the property name in the json data as in your class and you can handle the data correctly and easily, please refer on this link for more infos

Upvotes: 2

No need to convert. It s already json format but you should use array index then set user variable.

For example :

let user:any = {};
user = this.user[arrayIndex].user;

this.userData["user"] = user
this.userData["role"] = this.user[arrayIndex].role;

if you want o get data from json array , you should use array index. Like this.

let user:any = {};
user = this.user[0].user;
this.userData["role"] = this.user[0].role;

Upvotes: 0

Vivek Kumar
Vivek Kumar

Reputation: 5050

this.userData["role"] = this.user.role should be this.userData["role"] = this.role

Upvotes: 0

Related Questions