Dynamically Add Variable Name Value Pairs to JSON Object

how to add values to json object in javascript
append key value to json object
add key value pair to json object java
javascript array push key value pair dynamically
dynamically add key, value to object javascript
how to get dynamic key from json in javascript
add new key value pair to json object typescript
add property to array of objects typescript

I have a json object full of ips like

var ips = {}

I then add ip objects to this object like so

ips[ipID] = {}

I then need to add dynamic/variable name value pairs to each ip so I am using code like this

var name; var value; var temp = {};
tmp[name] = value

My question is, how can I add these name value pairs/ tmp to my ipID objects so that my outcome turns out like

ipID = { name : value, anotherName : anotherValue }

That's not JSON. It's just Javascript objects, and has nothing at all to do with JSON.

You can use brackets to set the properties dynamically. Example:

var obj = {};
obj['name'] = value;
obj['anotherName'] = anotherValue;

This gives exactly the same as creating the object with an object literal like this:

var obj = { name : value, anotherName : anotherValue };

If you have already added the object to the ips collection, you use one pair of brackets to access the object in the collection, and another pair to access the propery in the object:

ips[ipId] = {};
ips[ipId]['name'] = value;
ips[ipId]['anotherName'] = anotherValue;

Notice similarity with the code above, but that you are just using ips[ipId] instead of obj.

You can also get a reference to the object back from the collection, and use that to access the object while it remains in the collection:

ips[ipId] = {};
var obj = ips[ipId];
obj['name'] = value;
obj['anotherName'] = anotherValue;

You can use string variables to specify the names of the properties:

var name = 'name';
obj[name] = value;
name = 'anotherName';
obj[name] = anotherValue;

It's value of the variable (the string) that identifies the property, so while you use obj[name] for both properties in the code above, it's the string in the variable at the moment that you access it that determines what property will be accessed.

Add dynamic key, value pairs to JavaScript array or hash table , I'm trying to add a key value pair to an existing javascript associative array. The key needs to be a variable. This is for JSON encoding. I realize  How can I add a key/value pair to a JavaScript object? Object and want to add another Object containing key/value pairs, do this: add Object {name="Peanuts

With ECMAScript 6 there is a better way.

You can use computed property names in object property definitions, for example:

var name1 = 'John'; 
var value1 = '42'; 
var name2 = 'Sarah'; 
var value2 = '35';

var ipID = { 
             [name1] : value1, 
             [name2] : value2 

This is equivalent to the following, where you have variables for the property names.

var ipID = { 
             John: '42', 
             Sarah: '35' 

Dynamically creating properties on objects using javascript (Example), Properties on the Javascript object help set values that can be used within In javascript properties can be created by defining variables on a  Questions: From post: Sending a JSON array to be received as a Dictionary<string,string> I’m trying to do this same thing as that post. The only issue is that I don’t know what the keys and the values are upfront. So I need to be able to dynamically add the key and value pairs and I

when using javascript objects, you can also just use "dot notation" to add an item, (which JSLint prefers)

var myArray = { name : "john" };
//will initiate a key-value array with one item "name" and the value "john"
myArray.lastName = "smith";
//will add a key named lastName with the value "smith"
//Object {name: "john", lastName: "smith"}

Here is a screenshot from testing in the Chrome console

JavaScript, In order to set an object's key by variable, here are few steps to follow. innerHTML = JSON.stringify(myArray); Example-2:This example sets the object key by variable key as well as object value by variable val and then inserts {“​GFG_key”:”GFG_N_Val”} object to a How to use dynamic variable names in JavaScript ? @Mike: You can use a variable to specify the name of the property. It's the value of the variable at the moment of accessing the object that decides what property is accessed, so changing the variable to a different string between accesses is not a problem. See my addition above. – Guffa Nov 1 '10 at 18:20.

I'm assuming each entry in "ips" can have multiple name value pairs - so it's nested. You can achieve this data structure as such:

var ips = {}

function addIpId(ipID, name, value) {
    if (!ips[ipID]) ip[ipID] = {};
    var entries = ip[ipID];
    // you could add a check to ensure the name-value par's not already defined here
    var entries[name] = value;

JavaScript, In order to add Key/value pair to a JSON object, Either we use dot notation or square bracket notation. Both methods are widely accepted. Example 1: This  I do not need this extra space at end of value for each variable value. Also, it puts two spaces for blank character variables, I do not need that. I want nothing to be written to file when character or numeric variable is missing.

in Javascript.

    var myObject = { "name" : "john" };
    // { "name" : "john" };
    myObject.gender = "male";
    // { "name" : "john", "gender":"male"};

Adding items to a JSON object - JavaScript, I needed to add items to JSON object in a for loop. iterating over a numer of variables and adding their values to the JSON object: var events = [{}]; // empty JSON object var checks = dojo.query("input[name=item]"); var  JSON objects are surrounded by curly braces {}. JSON objects are written in key/value pairs. Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null). Keys and values are separated by a colon. Each key/value pair is separated by a comma.

Objects, A property is a “key: value” pair, where key is a string (also called a “property name”), It's easy to find a file by its name or add/remove a file. Here, the variable key may be calculated at run-time or depend on the user input. In javascript properties can be created by defining variables on a literal object. For example var obj = { property1: '', property2: '' }; Now one can access these properties by using obj.property1 = 'some value'; obj.property2 = 'some other value'; and similarly, they can also be used within functions that are within the object obj. For example:

Object initializer, You simply notate a list of key: value pairs delimited by commas. Oftentimes, there are variables in your code that you would like to put into an object. JSON.​parse() will reject computed property names and an error will be  The key value pairs will be used to attach arbitrary data to some other item. The data must be serialized as JSON to round-tip it through a system.

JavaScript object basics, As with many things in JavaScript, creating an object often begins with defining and initializing a variable. Each name/value pair must be separated by a comma, and the name One useful aspect of bracket notation is that it can be used to set not only member values dynamically, but member names too. Therefore, in serializing dynamic values into a JSON representation, values that JSON can't represent are serialized into string values. Conversely, Kusto will parse strings as strongly-typed values if they can be parsed as such. This applies for datetime, real, long, and guid types. For more about the JSON object model, see See json.org.

  • Do you also need code to produce the final JSON?
  • possible duplicate of Using a variable for a Javascript object key
  • This would work fine but my names are also variables. So when the name changes it overwrites the previous name value pair.
  • @Mike: You can use a variable to specify the name of the property. It's the value of the variable at the moment of accessing the object that decides what property is accessed, so changing the variable to a different string between accesses is not a problem. See my addition above.
  • Well, I am using a .change function to generate this and for some reason it keeps overwriting my name. Is there anyway to temporarily deference the variable?
  • Post code for the .change function and we can better help you.
  • pastie.org/1264618 as I mentioned, it only allows for one name value pair...
  • Is myArray really an array? It appears to be an Object or hash.
  • yep, myObject would have been a better name choice
  • I get an error "myObject.push is not a function" when I try that.