Get Value from JSON - Help

venkatkk
Tera Contributor

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

1 ACCEPTED SOLUTION

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


View solution in original post

7 REPLIES 7

Shishir Srivast
Mega Sage

You can use JSON.parse() to extract the value.



Please check this for more info: JSON.parse()


Shishir Srivast
Mega Sage

something like this may help, there shouldn't be space between the name, i have just put the underscore(_).



var text = '{"Cable":[{"USB":true},{"Displayport_to_HDMI":true}],"Carry_Case":[{"SwissgearPegasus_Backpack":true}]}';


var obj = JSON.parse(text);


gs.print(obj.Cable[1].Displayport_to_HDMI);


gs.print(obj.Carry_Case[0].SwissgearPegasus_Backpack);


Hi Shishir,



Underscore "_" is not required as long as the object key name is in double quotes.


For Ex.


{


        "Display to HDMI": true


}



the above representation is correct because key name is in double quotes



{


        Display_to_HDMI: true


}



this one is also going to work, for more details please check below URL.


javascript - What is the difference between object keys with quotes and without quotes? - Stack Over...



Thanks


Rahul


ChrisBurks
Giga Sage

Hi Venkat,



Since the object property contains spaces you'll have to use bracket notation instead of dot-walking to retrieve the value.



var obj = {"Cable":[{"USB":true},{"Displayport to HDMI":true}],"Carry Case":[{"SwissgearPegasus Backpack":true}]}



var displayPortToHDMI = obj["Displayport to HDMI"];


var swissgearPegasusBackpack = obj["SwissgearPegasus Backpack"]




Edit:



I forgot there's one more layer there from Cable which is an array of objects:



var displayPortToHDMI = obj.Cable[1]["Displayport to HDMI"];


var swissgearPegasusBackpack = obj.Cable[2]["SwissgearPegasus Backpack"];