Skip to main content

Getting key/value pair from JSON object and getting variable name and value from JavaScript object.

 Hi, I had faced one issue like this. I have an JSON object but I don't know any key name but I need to get the all the key and corresponding value from JSON object using client side JavaScript. Suddenly I wondered whether it's possible or not, after that I had done lot of workaround and finally got this solution. See the below example.
  function getKeyValueFromJSON() {
    var jsonObj = {a:10,b:20,c:30,d:50};
    for(var key in jsonObj) {
      alert("Key: " + key + " value: " + jsonObj[key]);

 In this example I have created the one json array as string, and converted this string into JSON object using eval() function. Using for-each loop I got all the key value from jsonObj, and finally using that key I got the corresponding value.
 Finally I got the alert like this,

   Key: a value:10
   Key: b value:20
   Key: c value:30
   Key: d value:50

 During this workaround I got one more idea, using this same way I got all the variable name and corresponding value from JavaScript object. See the below example.
 This is my JavaScript class. Here I have declared a function for variable d.
   function testJavaScriptClass() {
    this.a = 10;
    this.b = 20;
    this.c = 30;
    this.d= function() {
               return "Test result from inside this function";

 Now I am going to create one object for this class and going to get the variable name and corresponding value from JavaScript object.

 function getKeyValueFromJSObject() {
    var testObj = new testJavaScriptClass();
    for(var key in testObj) {
      if(typeof testObj[key] != "function") {
            alert("Key: " + key + " value: " + testObj[key]);   
      else {
            var value = testObj[key]();
            alert("Key: " + key + " value: " + value);

 Here I have checked the key (variable) is whether a function or not. If it is function then, I executed the function and get the result.
 Finally I got the alert this.

  Key: a value:10
  Key: b value:20
  Key: c value:30
  Key: d value: "Test result from inside this function"

Hope this help...!


PcGeek said…
I want to get it from mongodb console
to find the field name of the second record
Unknown said…
What if I have nested JSON like the below:

Gnanasuriyan said…
@Krishnamachari Shrikanth : For your input, we need to have recursive function.
Anonymous said…
It doesn't look like a javascript Object at all. you are simply using an array and looping thru the array.

Try looping thru this Object

var myObj = {id:1, name="joe", age=31", isAGenius=true}
Please help me on this..

JSON Response:

"Result1": {
"abc": 1,
"def": 2
"Result2": {
"abc": 3,
"def": 4

I need to know the following
1) length of CompleteResult
2) how to get the value "Result1" i.e, I may have Result3 in next response, I dont know what value will be coming there. So I have to get the value dynamically
Anonymous said…
Thank you!!! So simple, so elegant. You saved lot of my time.

Hi can u tell me how can access and take nested json key and values
Hi I have lot of json object in fouser table .For particular object say id is ObjectId("533a91b2263e03670b000009") taking means no problem working.I mean see the below map reduce concept in if condition[docid == ObjectId("533a91b2263e03670b000009")] hot coded.So working.

'collectionName' : 'fouser',
mapReduce : {
map : 'function () {
var docid = this._id.valueOf();

var date =;
var doc = this,
rec = doc.records;
if( docid == ObjectId("533a91b2263e03670b000009") ){
var i = 0;
Object.keys(rec).forEach(function(key) {

emit (NumberInt(i), { recordid : ObjectId(key) , completed : rec[key].completed, datedue:rec[key].due});
i = i+1;

reduce : 'function (key, values) {
var allDetails = [];
var user = { recordid: null, completed : null};
for( var index = 0; index < values.length; index++){
user.recordid = values[index].recordid;
user.completed = values[index].completed;
user = { recordid : null, completed : null};
return {list : allDetails}
out : 'list',



Instand of hot coding i passing dyanamically means showing error like "object.keys called on non object in object.keys Object.keys(rec).forEach(key) ".How can i resolve please help me .I'm using this inside jaspersoft
It has been simply incredibly generous with you to provide openly what exactly many individuals would’ve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.

Selenium Training in Chennai

Selenium Training in Bangalore
It has been simply incredibly generous with you to provide openly what exactly many individuals would’ve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.

Selenium Training in Chennai

Selenium Training in Bangalore
Unknown said…
Appreciation for really being thoughtful and also for deciding on certain marvelous guides most people really want to be aware of.

AWS Training in Chennai
Vicky Ram said…
Great blog!!! It was very impressed to me. I like so much and keep sharing. Thank you.

Hey, very nice site. I came across this on Google, and I am stoked that I did. I will definitely be coming back here more often. Wish I could add to the conversation and bring a bit more to the table, but am just taking in as much info as I can at the moment. Thanks .
This comment has been removed by the author.
priya sri said…
AWS training in hyderabad
AWS training will give the students obtain expertise in the theories of AMI Creation, EBS Persistent Storage, Amazon Storage Services S3, Route 53, AWS EC2 and AWS S3 Instances & further high-level concepts.
Yamini said…
Thank you for such a wonderful blog. It's a very great concept and I learn more details from your blog. Try
Elasticsearch Training
AWS Devops Training
CyberSecurity Training

Popular posts from this blog

Simple Login Application Using Spring MVC and Hibernate – Part 1

I hope developers working in web application development might hear about MVC architecture. Almost all technologies provide support for MVC based web application development, but the success is based on many factors like reusable of the code, maintenance of the code, future adaption of the code, etc..,  The success of the Spring MVC is “Open for extension and closed for modification” principle. Using Spring MVC the developers can easily develop MVC based web application. We don’t need any steep learning curve at the same time we need to know the basics of spring framework and MVC architecture. The Spring MVC consists of following important components. 1. Dispatcher servlet
2. Controller
3. View Resolver
4. Model Spring MVC - Overview  The overall architecture of Spring MVC is shown here.  1. When “Dispatcher Servlet” gets any request from client, it finds the corresponding mapped controller for the request and just dispatches the request to the corresponding controller. The reque…

Meteor tutorial - Part II - Building mobile applications

In my previous blog post, I have explained basics of meteor. In this tutorial I am going to add mobile support to the existing meteor application.

Installing mobile SDKs

Before going to start we need to install IOS and android SDKs (Software development kit). Here I am installing SDKs for iOS. Please keep in mind this is one time process.

For installing iOS SDKs, we need to invoke “meteor install-sdk ios”.

The same way you can install android SKDs by invoking “meteor install-sdk android” command.

Adding mobile platform support

I am using already created application called “myapp” and going to add iOS platform support by invoking a command “meteor add-platform ios”.

We can add android platform by invoking a command  “meteor add-platform android”.
Running application on mobile emulator
Once we add mobile platform then we can run our application by using real mobile device or emulator. I have added IOS platform to myapp already. Now I am going to run that application by using iPhone emulato…