Currently I have a JSON from which I want to extract certain specific data, for example I want to extract the
ID - Username - IP
Object from the JSON
Currently I have been trying with this code but it is not giving me the adequate results
var json = '42["getUsers",{"1620552418669":{"role":"guest","image":"/img/avatars/m/3.svg","startRoom":0,"expired":false,"free":false,"entries":"255042500","streamName":1620552397,"roles":{"canKick":"0","canBan":"0","canMute":false,"canSpyWhisperMessages":"0","canDeleteUserMessages":"0"},"country":"MX","webmasterid":1,"id":1620552418669,"username":"luis fdo5544","isGuest":true,"gender":"hombre","hasFlash":false,"hasWebcam":false,"hasWebrtc":true,"room":{"id":"47","name":"Más de 40","description":"La sala para mayores de 40 años.","welcome":"","date":"2019-12-07 16:12:02","destructionDate":"0000-00-00 00:00:00","webmasterid":"1","users":67,"password":"","image":"/upload/rooms/2tjdhjliih0ks0wggg.jpg","default":"0","webcam":"0","colorPicker":"1","isTemporary":"0","ownerid":"0","maxUsers":"150","reservedToGenderid":"0","orderRoom":"86","inPrivateConference":"0","isAdult":"0","reservedToRoles":"0","tags":"","country":"","numberFavorit":"298","approved":"0","deleted":"0","reservedToGenderidCondition":"Equals","reservedToRolesCondition":"Equals","urlImage":"","isHidden":"0","radio_url":"","radio_start":"1"},"webcam":false,"status":"online","webcamPublic":false,"privateOnlyOnInvitation":true,"credits":0,"e":"","isMobile":false,"fingerprint":1457712961,"enterChatMode":true,"friends":{},"doNotAcceptPrivate":true,"myFavouriteRooms":[],"oldRole":"guest","gotBackground":true,"roomsIn":[49,48,47],"rouletteBusy":true,"socketid":"VCqjrZXOyygqHUJIBcQO","ip":2806,"password":"","email":""},"1620555811311":{"role":"guest","image":"/img/avatars/m/18.svg","startRoom":0,"expired":false,"free":false,"entries":"255043519","streamName":1620555757,"roles":{"canKick":"0","canBan":"0","canMute":false,"canSpyWhisperMessages":"0","canDeleteUserMessages":"0"},"country":"SE","webmasterid":1,"id":1620555811311,"username":"xhip","isGuest":true,"gender":"hombre","hasFlash":false,"hasWebcam":false,"hasWebrtc":true,"room":{"id":"48","name":"Más de 50","description":"La sala para mayores de 50 años.","welcome":"","date":"2019-12-07 16:12:30","destructionDate":"0000-00-00 00:00:00","webmasterid":"1","users":1,"password":"","image":"/upload/rooms/s7eqp5bzxn4844c844.png","default":"0","webcam":"0","colorPicker":"1","isTemporary":"0","ownerid":"0","maxUsers":"115","reservedToGenderid":"0","orderRoom":"84","inPrivateConference":"0","isAdult":"0","reservedToRoles":"0","tags":"","country":"","numberFavorit":"234","approved":"0","deleted":"0","reservedToGenderidCondition":"Equals","reservedToRolesCondition":"Equals","urlImage":"","isHidden":"0","radio_url":"","radio_start":"1"},"webcam":false,"status":"online","webcamPublic":false,"privateOnlyOnInvitation":true,"credits":0,"e":"","isMobile":false,"fingerprint":-1200890177,"enterChatMode":true,"friends":{},"doNotAcceptPrivate":true,"myFavouriteRooms":[],"oldRole":"guest","rouletteBusy":true,"socketid":"mOPdOA1S2QDKozzKBcju","ip":3113473371,"roomsIn":[48],"password":"","email":""}},"48"]';
var parsed = JSON.parse(json.substr(2, json.length));
var newjson = (parsed);
newjson.forEach((obj) => {
for (const [key, value] of Object.entries(obj)) {
console.log(`${key} - ${value}`);
}
});
Does anyone know how I can correct the code and get the expected results?.
Thanks.
You should be accessing the inner element of the parsed array first and from there extract the property you want.
var json = '42["getUsers",{"1620552418669":{"role":"guest","image":"/img/avatars/m/3.svg","startRoom":0,"expired":false,"free":false,"entries":"255042500","streamName":1620552397,"roles":{"canKick":"0","canBan":"0","canMute":false,"canSpyWhisperMessages":"0","canDeleteUserMessages":"0"},"country":"MX","webmasterid":1,"id":1620552418669,"username":"luis fdo5544","isGuest":true,"gender":"hombre","hasFlash":false,"hasWebcam":false,"hasWebrtc":true,"room":{"id":"47","name":"Más de 40","description":"La sala para mayores de 40 años.","welcome":"","date":"2019-12-07 16:12:02","destructionDate":"0000-00-00 00:00:00","webmasterid":"1","users":67,"password":"","image":"/upload/rooms/2tjdhjliih0ks0wggg.jpg","default":"0","webcam":"0","colorPicker":"1","isTemporary":"0","ownerid":"0","maxUsers":"150","reservedToGenderid":"0","orderRoom":"86","inPrivateConference":"0","isAdult":"0","reservedToRoles":"0","tags":"","country":"","numberFavorit":"298","approved":"0","deleted":"0","reservedToGenderidCondition":"Equals","reservedToRolesCondition":"Equals","urlImage":"","isHidden":"0","radio_url":"","radio_start":"1"},"webcam":false,"status":"online","webcamPublic":false,"privateOnlyOnInvitation":true,"credits":0,"e":"","isMobile":false,"fingerprint":1457712961,"enterChatMode":true,"friends":{},"doNotAcceptPrivate":true,"myFavouriteRooms":[],"oldRole":"guest","gotBackground":true,"roomsIn":[49,48,47],"rouletteBusy":true,"socketid":"VCqjrZXOyygqHUJIBcQO","ip":2806,"password":"","email":""},"1620555811311":{"role":"guest","image":"/img/avatars/m/18.svg","startRoom":0,"expired":false,"free":false,"entries":"255043519","streamName":1620555757,"roles":{"canKick":"0","canBan":"0","canMute":false,"canSpyWhisperMessages":"0","canDeleteUserMessages":"0"},"country":"SE","webmasterid":1,"id":1620555811311,"username":"xhip","isGuest":true,"gender":"hombre","hasFlash":false,"hasWebcam":false,"hasWebrtc":true,"room":{"id":"48","name":"Más de 50","description":"La sala para mayores de 50 años.","welcome":"","date":"2019-12-07 16:12:30","destructionDate":"0000-00-00 00:00:00","webmasterid":"1","users":1,"password":"","image":"/upload/rooms/s7eqp5bzxn4844c844.png","default":"0","webcam":"0","colorPicker":"1","isTemporary":"0","ownerid":"0","maxUsers":"115","reservedToGenderid":"0","orderRoom":"84","inPrivateConference":"0","isAdult":"0","reservedToRoles":"0","tags":"","country":"","numberFavorit":"234","approved":"0","deleted":"0","reservedToGenderidCondition":"Equals","reservedToRolesCondition":"Equals","urlImage":"","isHidden":"0","radio_url":"","radio_start":"1"},"webcam":false,"status":"online","webcamPublic":false,"privateOnlyOnInvitation":true,"credits":0,"e":"","isMobile":false,"fingerprint":-1200890177,"enterChatMode":true,"friends":{},"doNotAcceptPrivate":true,"myFavouriteRooms":[],"oldRole":"guest","rouletteBusy":true,"socketid":"mOPdOA1S2QDKozzKBcju","ip":3113473371,"roomsIn":[48],"password":"","email":""}},"48"]';
var parsed = JSON.parse(json.substr(2, json.length));
var obj = parsed[1]
var extracted = Object.keys(obj).map(key => {
const { id, username, ip } = obj[key]
return { id, username, ip };
})
console.log(extracted);
You can get the user objects as an array using Object.values(parsed[1])
var json = getString();
var parsed = JSON.parse(json.substr(2, json.length));
const users = Object.values(parsed[1]).map(({id,username,ip })=>({id,username,ip}))
console.log(users)
function getString(){
return '42["getUsers",{"1620552418669":{"role":"guest","image":"/img/avatars/m/3.svg","startRoom":0,"expired":false,"free":false,"entries":"255042500","streamName":1620552397,"roles":{"canKick":"0","canBan":"0","canMute":false,"canSpyWhisperMessages":"0","canDeleteUserMessages":"0"},"country":"MX","webmasterid":1,"id":1620552418669,"username":"luis fdo5544","isGuest":true,"gender":"hombre","hasFlash":false,"hasWebcam":false,"hasWebrtc":true,"room":{"id":"47","name":"Más de 40","description":"La sala para mayores de 40 años.","welcome":"","date":"2019-12-07 16:12:02","destructionDate":"0000-00-00 00:00:00","webmasterid":"1","users":67,"password":"","image":"/upload/rooms/2tjdhjliih0ks0wggg.jpg","default":"0","webcam":"0","colorPicker":"1","isTemporary":"0","ownerid":"0","maxUsers":"150","reservedToGenderid":"0","orderRoom":"86","inPrivateConference":"0","isAdult":"0","reservedToRoles":"0","tags":"","country":"","numberFavorit":"298","approved":"0","deleted":"0","reservedToGenderidCondition":"Equals","reservedToRolesCondition":"Equals","urlImage":"","isHidden":"0","radio_url":"","radio_start":"1"},"webcam":false,"status":"online","webcamPublic":false,"privateOnlyOnInvitation":true,"credits":0,"e":"","isMobile":false,"fingerprint":1457712961,"enterChatMode":true,"friends":{},"doNotAcceptPrivate":true,"myFavouriteRooms":[],"oldRole":"guest","gotBackground":true,"roomsIn":[49,48,47],"rouletteBusy":true,"socketid":"VCqjrZXOyygqHUJIBcQO","ip":2806,"password":"","email":""},"1620555811311":{"role":"guest","image":"/img/avatars/m/18.svg","startRoom":0,"expired":false,"free":false,"entries":"255043519","streamName":1620555757,"roles":{"canKick":"0","canBan":"0","canMute":false,"canSpyWhisperMessages":"0","canDeleteUserMessages":"0"},"country":"SE","webmasterid":1,"id":1620555811311,"username":"xhip","isGuest":true,"gender":"hombre","hasFlash":false,"hasWebcam":false,"hasWebrtc":true,"room":{"id":"48","name":"Más de 50","description":"La sala para mayores de 50 años.","welcome":"","date":"2019-12-07 16:12:30","destructionDate":"0000-00-00 00:00:00","webmasterid":"1","users":1,"password":"","image":"/upload/rooms/s7eqp5bzxn4844c844.png","default":"0","webcam":"0","colorPicker":"1","isTemporary":"0","ownerid":"0","maxUsers":"115","reservedToGenderid":"0","orderRoom":"84","inPrivateConference":"0","isAdult":"0","reservedToRoles":"0","tags":"","country":"","numberFavorit":"234","approved":"0","deleted":"0","reservedToGenderidCondition":"Equals","reservedToRolesCondition":"Equals","urlImage":"","isHidden":"0","radio_url":"","radio_start":"1"},"webcam":false,"status":"online","webcamPublic":false,"privateOnlyOnInvitation":true,"credits":0,"e":"","isMobile":false,"fingerprint":-1200890177,"enterChatMode":true,"friends":{},"doNotAcceptPrivate":true,"myFavouriteRooms":[],"oldRole":"guest","rouletteBusy":true,"socketid":"mOPdOA1S2QDKozzKBcju","ip":3113473371,"roomsIn":[48],"password":"","email":""}},"48"]';
}
In your implementation what you are getting after parsing is an array, and the required fields such as id, username & ip are present in the object present at index 1 of the parsed string.
Since the value present at index 1 is an object we can make use of Object.keys or Object.values or Object.entries as per the need. In the below example I've used Object.values to get the required output.
const json = '42["getUsers",{"1620552418669":{"role":"guest","image":"/img/avatars/m/3.svg","startRoom":0,"expired":false,"free":false,"entries":"255042500","streamName":1620552397,"roles":{"canKick":"0","canBan":"0","canMute":false,"canSpyWhisperMessages":"0","canDeleteUserMessages":"0"},"country":"MX","webmasterid":1,"id":1620552418669,"username":"luis fdo5544","isGuest":true,"gender":"hombre","hasFlash":false,"hasWebcam":false,"hasWebrtc":true,"room":{"id":"47","name":"Más de 40","description":"La sala para mayores de 40 años.","welcome":"","date":"2019-12-07 16:12:02","destructionDate":"0000-00-00 00:00:00","webmasterid":"1","users":67,"password":"","image":"/upload/rooms/2tjdhjliih0ks0wggg.jpg","default":"0","webcam":"0","colorPicker":"1","isTemporary":"0","ownerid":"0","maxUsers":"150","reservedToGenderid":"0","orderRoom":"86","inPrivateConference":"0","isAdult":"0","reservedToRoles":"0","tags":"","country":"","numberFavorit":"298","approved":"0","deleted":"0","reservedToGenderidCondition":"Equals","reservedToRolesCondition":"Equals","urlImage":"","isHidden":"0","radio_url":"","radio_start":"1"},"webcam":false,"status":"online","webcamPublic":false,"privateOnlyOnInvitation":true,"credits":0,"e":"","isMobile":false,"fingerprint":1457712961,"enterChatMode":true,"friends":{},"doNotAcceptPrivate":true,"myFavouriteRooms":[],"oldRole":"guest","gotBackground":true,"roomsIn":[49,48,47],"rouletteBusy":true,"socketid":"VCqjrZXOyygqHUJIBcQO","ip":2806,"password":"","email":""},"1620555811311":{"role":"guest","image":"/img/avatars/m/18.svg","startRoom":0,"expired":false,"free":false,"entries":"255043519","streamName":1620555757,"roles":{"canKick":"0","canBan":"0","canMute":false,"canSpyWhisperMessages":"0","canDeleteUserMessages":"0"},"country":"SE","webmasterid":1,"id":1620555811311,"username":"xhip","isGuest":true,"gender":"hombre","hasFlash":false,"hasWebcam":false,"hasWebrtc":true,"room":{"id":"48","name":"Más de 50","description":"La sala para mayores de 50 años.","welcome":"","date":"2019-12-07 16:12:30","destructionDate":"0000-00-00 00:00:00","webmasterid":"1","users":1,"password":"","image":"/upload/rooms/s7eqp5bzxn4844c844.png","default":"0","webcam":"0","colorPicker":"1","isTemporary":"0","ownerid":"0","maxUsers":"115","reservedToGenderid":"0","orderRoom":"84","inPrivateConference":"0","isAdult":"0","reservedToRoles":"0","tags":"","country":"","numberFavorit":"234","approved":"0","deleted":"0","reservedToGenderidCondition":"Equals","reservedToRolesCondition":"Equals","urlImage":"","isHidden":"0","radio_url":"","radio_start":"1"},"webcam":false,"status":"online","webcamPublic":false,"privateOnlyOnInvitation":true,"credits":0,"e":"","isMobile":false,"fingerprint":-1200890177,"enterChatMode":true,"friends":{},"doNotAcceptPrivate":true,"myFavouriteRooms":[],"oldRole":"guest","rouletteBusy":true,"socketid":"mOPdOA1S2QDKozzKBcju","ip":3113473371,"roomsIn":[48],"password":"","email":""}},"48"]';
const parsed = JSON.parse(json.substr(2, json.length));
const newjson = parsed[1];
Object.values(newjson).forEach(valueObj => {
const { id, ip, username } = valueObj;
console.log(`id: ${id}`);
console.log(`username: ${username}`);
console.log(`ip: ${id}`);
});
For a more dynamic version, if you want to pass the keys for which you want to get the data we can use the below method.
var json = '42["getUsers",{"1620552418669":{"role":"guest","image":"/img/avatars/m/3.svg","startRoom":0,"expired":false,"free":false,"entries":"255042500","streamName":1620552397,"roles":{"canKick":"0","canBan":"0","canMute":false,"canSpyWhisperMessages":"0","canDeleteUserMessages":"0"},"country":"MX","webmasterid":1,"id":1620552418669,"username":"luis fdo5544","isGuest":true,"gender":"hombre","hasFlash":false,"hasWebcam":false,"hasWebrtc":true,"room":{"id":"47","name":"Más de 40","description":"La sala para mayores de 40 años.","welcome":"","date":"2019-12-07 16:12:02","destructionDate":"0000-00-00 00:00:00","webmasterid":"1","users":67,"password":"","image":"/upload/rooms/2tjdhjliih0ks0wggg.jpg","default":"0","webcam":"0","colorPicker":"1","isTemporary":"0","ownerid":"0","maxUsers":"150","reservedToGenderid":"0","orderRoom":"86","inPrivateConference":"0","isAdult":"0","reservedToRoles":"0","tags":"","country":"","numberFavorit":"298","approved":"0","deleted":"0","reservedToGenderidCondition":"Equals","reservedToRolesCondition":"Equals","urlImage":"","isHidden":"0","radio_url":"","radio_start":"1"},"webcam":false,"status":"online","webcamPublic":false,"privateOnlyOnInvitation":true,"credits":0,"e":"","isMobile":false,"fingerprint":1457712961,"enterChatMode":true,"friends":{},"doNotAcceptPrivate":true,"myFavouriteRooms":[],"oldRole":"guest","gotBackground":true,"roomsIn":[49,48,47],"rouletteBusy":true,"socketid":"VCqjrZXOyygqHUJIBcQO","ip":2806,"password":"","email":""},"1620555811311":{"role":"guest","image":"/img/avatars/m/18.svg","startRoom":0,"expired":false,"free":false,"entries":"255043519","streamName":1620555757,"roles":{"canKick":"0","canBan":"0","canMute":false,"canSpyWhisperMessages":"0","canDeleteUserMessages":"0"},"country":"SE","webmasterid":1,"id":1620555811311,"username":"xhip","isGuest":true,"gender":"hombre","hasFlash":false,"hasWebcam":false,"hasWebrtc":true,"room":{"id":"48","name":"Más de 50","description":"La sala para mayores de 50 años.","welcome":"","date":"2019-12-07 16:12:30","destructionDate":"0000-00-00 00:00:00","webmasterid":"1","users":1,"password":"","image":"/upload/rooms/s7eqp5bzxn4844c844.png","default":"0","webcam":"0","colorPicker":"1","isTemporary":"0","ownerid":"0","maxUsers":"115","reservedToGenderid":"0","orderRoom":"84","inPrivateConference":"0","isAdult":"0","reservedToRoles":"0","tags":"","country":"","numberFavorit":"234","approved":"0","deleted":"0","reservedToGenderidCondition":"Equals","reservedToRolesCondition":"Equals","urlImage":"","isHidden":"0","radio_url":"","radio_start":"1"},"webcam":false,"status":"online","webcamPublic":false,"privateOnlyOnInvitation":true,"credits":0,"e":"","isMobile":false,"fingerprint":-1200890177,"enterChatMode":true,"friends":{},"doNotAcceptPrivate":true,"myFavouriteRooms":[],"oldRole":"guest","rouletteBusy":true,"socketid":"mOPdOA1S2QDKozzKBcju","ip":3113473371,"roomsIn":[48],"password":"","email":""}},"48"]';
const parsed = JSON.parse(json.substr(2, json.length));
const newjson = parsed[1];
const getValues = (dataObj, keys) => Object.values(dataObj).map(valueObj => {
const obj = {}
keys.forEach(key => {
obj[key] = valueObj[key];
});
return obj;
});
console.log(getValues(newjson, ["id", "username", "ip"]));
You may try this one.
var json =
'42["getUsers",{"1620552418669":{"role":"guest","image":"/img/avatars/m/3.svg","startRoom":0,"expired":false,"free":false,"entries":"255042500","streamName":1620552397,"roles":{"canKick":"0","canBan":"0","canMute":false,"canSpyWhisperMessages":"0","canDeleteUserMessages":"0"},"country":"MX","webmasterid":1,"id":1620552418669,"username":"luis fdo5544","isGuest":true,"gender":"hombre","hasFlash":false,"hasWebcam":false,"hasWebrtc":true,"room":{"id":"47","name":"Más de 40","description":"La sala para mayores de 40 años.","welcome":"","date":"2019-12-07 16:12:02","destructionDate":"0000-00-00 00:00:00","webmasterid":"1","users":67,"password":"","image":"/upload/rooms/2tjdhjliih0ks0wggg.jpg","default":"0","webcam":"0","colorPicker":"1","isTemporary":"0","ownerid":"0","maxUsers":"150","reservedToGenderid":"0","orderRoom":"86","inPrivateConference":"0","isAdult":"0","reservedToRoles":"0","tags":"","country":"","numberFavorit":"298","approved":"0","deleted":"0","reservedToGenderidCondition":"Equals","reservedToRolesCondition":"Equals","urlImage":"","isHidden":"0","radio_url":"","radio_start":"1"},"webcam":false,"status":"online","webcamPublic":false,"privateOnlyOnInvitation":true,"credits":0,"e":"","isMobile":false,"fingerprint":1457712961,"enterChatMode":true,"friends":{},"doNotAcceptPrivate":true,"myFavouriteRooms":[],"oldRole":"guest","gotBackground":true,"roomsIn":[49,48,47],"rouletteBusy":true,"socketid":"VCqjrZXOyygqHUJIBcQO","ip":2806,"password":"","email":""},"1620555811311":{"role":"guest","image":"/img/avatars/m/18.svg","startRoom":0,"expired":false,"free":false,"entries":"255043519","streamName":1620555757,"roles":{"canKick":"0","canBan":"0","canMute":false,"canSpyWhisperMessages":"0","canDeleteUserMessages":"0"},"country":"SE","webmasterid":1,"id":1620555811311,"username":"xhip","isGuest":true,"gender":"hombre","hasFlash":false,"hasWebcam":false,"hasWebrtc":true,"room":{"id":"48","name":"Más de 50","description":"La sala para mayores de 50 años.","welcome":"","date":"2019-12-07 16:12:30","destructionDate":"0000-00-00 00:00:00","webmasterid":"1","users":1,"password":"","image":"/upload/rooms/s7eqp5bzxn4844c844.png","default":"0","webcam":"0","colorPicker":"1","isTemporary":"0","ownerid":"0","maxUsers":"115","reservedToGenderid":"0","orderRoom":"84","inPrivateConference":"0","isAdult":"0","reservedToRoles":"0","tags":"","country":"","numberFavorit":"234","approved":"0","deleted":"0","reservedToGenderidCondition":"Equals","reservedToRolesCondition":"Equals","urlImage":"","isHidden":"0","radio_url":"","radio_start":"1"},"webcam":false,"status":"online","webcamPublic":false,"privateOnlyOnInvitation":true,"credits":0,"e":"","isMobile":false,"fingerprint":-1200890177,"enterChatMode":true,"friends":{},"doNotAcceptPrivate":true,"myFavouriteRooms":[],"oldRole":"guest","rouletteBusy":true,"socketid":"mOPdOA1S2QDKozzKBcju","ip":3113473371,"roomsIn":[48],"password":"","email":""}},"48"]';
var parsed = JSON.parse(json.substr(2, json.length));
var newjson = parsed;
for (const key in newjson) {
if (Object.hasOwnProperty.call(newjson, key)) {
const element = newjson[key];
var keys = Object.keys(element);
keys.forEach((el) => {
if (Object.hasOwnProperty.call(element[el], "role")) {
for (const [key, value] of Object.entries(element[el])) {
console.log(`${key} - ${value}`);
}
}
});
}
}