Link to home
Start Free TrialLog in
Avatar of Jayesh Acharya
Jayesh AcharyaFlag for United States of America

asked on

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"}
Avatar of Jan Louwerens
Jan Louwerens
Flag of United States of America image

Try:

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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jan Louwerens
Jan Louwerens
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial