0
var twitchProfile = {};
request("https://api.twitch.tv/kraken/users/magnaboyy.json?client_id=8gkbch3ffqj35c1ccx088b9j6leifs8", function (error, response, body) {
const TwitchURL = body;
var jsonContent = JSON.parse(TwitchURL);
twitchProfile.display_name = (jsonContent.display_name);
twitchProfile.id = (jsonContent.id);
twitchProfile.name = (jsonContent.name);
twitchProfile.created_at = (jsonContent.created_at);
twitchProfile.logo = (jsonContent.logo);
console.log(twitchProfile);

In my code, I am grabbing the HTML body of the page (which is the json object) and then i am parsing each key in the object as a key in a JS object, considering I dont want to change anything in the names of the keys, or the content of the object, is there a way to convert the entire object over in one go, instead of all the components of it at a time? If not, am I doing it in a good way?

Magnaboy
  • 13
  • 1
  • 4

4 Answers4

2

Yes, you can just assign parsed object to your variable:

var twitchProfile = {};
request("https://api.twitch.tv/kraken/users/magnaboyy.json?client_id=8gkbch3ffqj35c1ccx088b9j6leifs8", function (error, response, body) {
    const TwitchURL = body;
    var twitchProfile = JSON.parse(TwitchURL);
    console.log(twitchProfile);
}

Also if your 'TwitchProfile' already has some properties you have to merge objects. Method for merging described here: How can I merge properties of two JavaScript objects dynamically?

Community
  • 1
  • 1
Alex Slipknot
  • 2,439
  • 1
  • 18
  • 26
  • thank you, this makes sense, ive spent over an hour trying to further debug my code to work in async but its not working: https://jsfiddle.net/kecqpn48/ The request is inside the try, but ive tried moving it outside, using r.get, using rp.get(request-promise) and have no idea whats causing it(ive tried LOTS of solutions). – Magnaboy Apr 19 '17 at 08:28
  • @Magnaboy jsfiddle doesn't work cause of missing includes. Did you see examples of module **request**? https://github.com/request/request – Alex Slipknot Apr 19 '17 at 08:33
  • I was just using jsfiddle as a way to show you the code, I've seen examples yes – Magnaboy Apr 19 '17 at 09:27
  • it gives the second message in the code 'ERROR', do you have experience working with async javascript? the issue is with improper async syntax/formatting i think. – Magnaboy Apr 19 '17 at 11:20
  • The code example is ok. But I saw that you're using await in jsfiddle. Please launch code without await first to see that response and parse is ok – Alex Slipknot Apr 19 '17 at 11:23
1

If you have different objects but need merge them together, in example:

var a = {
 x:0,
 y:1,
 z:2
}

var fromJSON = {
 x:11,
 y:12,
}

you can use Object.Assign():

var a = {
 x:0,
 y:1,
 z:2
}

var fromJSON = {
 x:11,
 y:12,
}

var assigned = Object.Assign(a,fromJSON);
console.log(assigned);

// assigned = {
//  x:11,
//  y:12,
//  z:2
// }
0

No, the only thing you could do to make it more readable is to use a constructor.

See following example please:

var jsonObj = {
  "name" : "Alessandro",
  "surname" : "Test",
  "age" : 34
};

function Person(json){
  this.name = json.name;
  this.surname = json.surname;
  this.age = json.age;
}

var person1 = new Person(jsonObj);

console.log("Hi " + person1.name);
Alessandro
  • 4,382
  • 8
  • 36
  • 70
0

ways to achieve this :

1. You can create deep copy of the source object into the destination(new) object using JSON.parse() and JSON.stringify() methods of JavaScript.

var jsonObj = {
  "display_name" : "alpha",
  "id" : 1,
  "name" : "xyz",
  "logo" : "blablabla"
};

var twitchProfile = JSON.parse(JSON.stringify(jsonObj));

console.log(twitchProfile);

2. Using ES6 Spread operator and Destructing Assignment.

const jsonObj = {
  "display_name" : "alpha",
  "id" : 1,
  "name" : "xyz",
  "logo" : "blablabla"
};
const {...twitchProfile} = jsonObj;
console.log(twitchProfile);

3. Iterate the source object using for...in loop and assign the properties into destination(new) object.

var jsonObj = {
  "display_name" : "alpha",
  "id" : 1,
  "name" : "xyz",
  "logo" : "blablabla"
};

var twitchProfile = {};

for (var i in jsonObj) {
  twitchProfile[i] = jsonObj[i];
}

console.log(twitchProfile);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123