- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2018 08:35 PM
I have a JSON string received from some of the checkbox as below
{"Cable":[{"USB":true},{"Displayport to HDMI":true}],"Carry Case":[{"SwissgearPegasus Backpack":true}]}
I want to get the highlighted value and store in another catalog variable. How can we achieve this ..Any help
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-22-2018 08:51 AM
Hi Venkat,
In your script you've only done one iteration. However, the object contains values that are arrays themselves which it looks like you've tried to compensate for but then those arrays contain objects with their own properties that you would need to collect and iterate through.
I don't know how you'll be using this data and if it will be a consistent format. But one way you could do this is convert the json object into a single object as well as converting the spaced string properties with underscores.
It may not be the most sufficient but then again I don't know how you will need to use this data:
var string = '{"Cable":[{"2":"2","USB to RS":true},{"1":"1","USB":true}],"Other":[{"1":"1","HTC Vive VR Headset":true}]}';
var obj = JSON.parse(string);
var keysArray = Object.keys(obj);
var singleObj = {};
var converToSingleObj = keysArray.map(function(item){
//this map will return two arrays
// [{"2":"2","USB to RS":true},{"1":"1","USB":true}]
//and
// [{"1":"1","HTC Vive VR Headset":true}]
// respectively from the two keys "Cable" and "Other"
return obj[item];
}).forEach(function(item){
//iterate through and perform forEach with function until we get one object
return item.forEach(function(item){
var newObj = item;
var keys = Object.keys(newObj);
keys = keys.forEach(function(item){
//if the numbered properties of no use take them out
if( Number.isNaN(Number(item))){
//stuff everything in the singleObj with only desired properites and values
//and replacing spaces with undersores for the properties
singleObj[item.replace(/ /g, "_")] = newObj[item];
}
})
})
});
console.log(singleObj) // {USB_to_RS: true, USB: true, HTC_Vive_VR_Headset: true}
Note: convertToSingleObj will be undefined as forEach doesn't return anything
hope that helps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-22-2018 01:41 AM
Thanks for the feedback , i tried to retrieve values with the keys but i cannot
var string = '{"Cable":[{"2":"2","USB to RS":true},{"1":"1","USB":true}],"Other":[{"1":"1","HTC Vive VR Headset":true}]}';
var obj = JSON.parse(string);
var keysArray = Object.keys(obj);
console.log(keysArray);
for (var i = 0; i < keysArray.length; i++) {
var key = keysArray[i];
var value = obj[keysArray[0][obj[keys[0]]]]; /// iam doing wrong here any help
console.log(key);
console.log(value);
}
I want to retrieve each value in the array ( eg :- USB to RS , USB, HTC Vive VR Headset)
How can we do that?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-22-2018 08:51 AM
Hi Venkat,
In your script you've only done one iteration. However, the object contains values that are arrays themselves which it looks like you've tried to compensate for but then those arrays contain objects with their own properties that you would need to collect and iterate through.
I don't know how you'll be using this data and if it will be a consistent format. But one way you could do this is convert the json object into a single object as well as converting the spaced string properties with underscores.
It may not be the most sufficient but then again I don't know how you will need to use this data:
var string = '{"Cable":[{"2":"2","USB to RS":true},{"1":"1","USB":true}],"Other":[{"1":"1","HTC Vive VR Headset":true}]}';
var obj = JSON.parse(string);
var keysArray = Object.keys(obj);
var singleObj = {};
var converToSingleObj = keysArray.map(function(item){
//this map will return two arrays
// [{"2":"2","USB to RS":true},{"1":"1","USB":true}]
//and
// [{"1":"1","HTC Vive VR Headset":true}]
// respectively from the two keys "Cable" and "Other"
return obj[item];
}).forEach(function(item){
//iterate through and perform forEach with function until we get one object
return item.forEach(function(item){
var newObj = item;
var keys = Object.keys(newObj);
keys = keys.forEach(function(item){
//if the numbered properties of no use take them out
if( Number.isNaN(Number(item))){
//stuff everything in the singleObj with only desired properites and values
//and replacing spaces with undersores for the properties
singleObj[item.replace(/ /g, "_")] = newObj[item];
}
})
})
});
console.log(singleObj) // {USB_to_RS: true, USB: true, HTC_Vive_VR_Headset: true}
Note: convertToSingleObj will be undefined as forEach doesn't return anything
hope that helps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-23-2018 06:44 AM
Thanks Chris...It helped me to achieve what i need.
Once again thanks for your help