• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 59
  • Last Modified:

dynamically assign key in json using javasctipt

I am trying to find a way to dynamically assign keynames when creating my json object using java script.
This issue I have is that the stringfy option ignores the value for the key name but it does interpet the key value
..
..

  var xxKeyName = "MyTestKey1"
  var xxKeyValue = "12345"

   var oTableDataJson =
        JSON.stringify(
            {xxKeyName : xxKeyValue});
..
this result in a json looking like:
{"xxKeyName":"12345"}

but what I want is
{"MyTestKey1":"12345"}
0
Jayesh Acharya
Asked:
Jayesh Acharya
  • 2
2 Solutions
 
Jan LouwerensSoftware EngineerCommented:
Try:

var oTableDataJson = 
        JSON.stringify(
            {"name": xxKeyName, "value" : xxKeyValue});

Open in new window

0
 
Jan LouwerensSoftware EngineerCommented:
Another option is to create an object first, and dynamically set the fields:

var xxKeyName = "MyTestKey1"
var xxKeyValue = "12345"

var obj = {};
obj[xxKeyName] = xxKeyValue;

var oTableDataJson = JSON.stringify(obj);

Open in new window

1
 
Julian HansenCommented:
Just a sidebar because the question is misleading. JSON is a string representation of a JavaScript object - you cannot assign a key in JSON - you create the key in JavaScript and then stringify the result.

Then just a word or two on how JavaScript objects work
The left hand side (key) of an object declaration is the name of the key - you cannot specify a variable name - it will not be interpreted as such.

To assign a key dynamically you have to use Array notation (as Jan demonstrated in his second post). While this looks like an associative array has been created it is actually implemented as an object.

So
var x = {};
x['test'] = 12345;
console.log(x);

Open in new window


Will produce the following output
Object { test: 12345 }

Open in new window


What if we did this though
var y = [];
y['test'] = 'abc';

Open in new window

This creates something that is a bit of a mix. An array that also has properties
If we dump this it produces
[]
length: 0
test: "abc"
__proto__: Array []

Open in new window

If we add the following
y.push(1);

Open in new window

We get
[…]
0: 1
length: 1
test: "abc"
__proto__: Array []

Open in new window


So from this we can see that to generate a pure object with dynamic keys we declare the variable as an object first

var myObj = {}

Open in new window


And then use "array notation" to assign the key
var keyname = 'MyDynamicKey';
var value = '123';
myObj[keyname] = value;

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now