Use string concatenation.

String message = "this is value  want to pass to the ActualMessage attribute " ;
String input = "{\r\n" + 
               "\"Level\": 0,\r\n" + 
               "\"Name\": \"String\",\r\n" + 
               "\"msgName\": \"String\",\r\n" + 
               "\"ActualMessage\": \"" + message + "\",\r\n" + 
               "\"TimeStamp\": \"/Date(-62135596800000-0000)/\"\r\n" + 
               "}" ;
Answer from chockleyc on Stack Overflow
🌐
InterSystems
docs.intersystems.com › irislatest › csp › docbook › DocBook.UI.Page.cls
Creating and Modifying Dynamic Entities | Using JSON | InterSystems IRIS Data Platform 2025.3
Dynamic arrays are zero-based. Object properties are addressed by property names. Although property names are string literals, double quotes are optional if the property name is a valid class member name. If the specified entity member does not yet exist, it will be created when you assign a value to it. As previously mentioned, values are always stored and retrieved in ObjectScript format regardless of how they are represented in JSON syntax.
🌐
ServiceNow Community
servicenow.com › community › developer-forum › set-dynamic-key-value-in-json › m-p › 1467969
Solved: Set dynamic key value in json - ServiceNow Community
August 6, 2019 - var primaryObj = source.u_bp_primary_object_type.toString(); //dynamic values var secondayObj = source.u_bp_secondary_object_type.toString(); //dynamic values var payload = { "app_key": appKey.toString(), "status": source.u_bp_incident_status.toString(), "servicenow_ticket_created": "false" }; payload[primaryObj] = source.u_bp_primary_objects.toString(); payload[secondayObj] = source.u_bp_secondary_objects.toString(); ... var dynamicKey = "value3"; var payload = { "value1": "text1", "value2" : "text2", }; payload[dynamicKey] = "text3"; gs.print(JSON.stringify(payload));
Top answer
1 of 8
212

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.

2 of 8
37

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' 
           }
🌐
EyeHunts
tutorial.eyehunts.com › home › how to pass dynamic value in json file
How to pass dynamic value in json file - Tutorial - By EyeHunts
June 15, 2023 - const fs = require('fs'); // Example dynamic value const dynamicValue = "OpenAI"; // Load the JSON file const jsonData = fs.readFileSync('data.json', 'utf8'); const data = JSON.parse(jsonData); // Modify the data structure data.field_name = ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › c language › cjson-json-file-write-read-modify-in-c
cJSON - JSON File Write/Read/Modify in C - GeeksforGeeks
April 28, 2025 - We then add key-value pairs to the object using the `cJSON_AddStringToObject()` and `cJSON_AddNumberToObject()` functions. We then convert the cJSON object to a JSON string using the `cJSON_Print()` function, which returns a dynamically allocated ...
🌐
InterSystems
docs.intersystems.com › latest › csp › docbook › DocBook.UI.Page.cls
Creating and Modifying Dynamic Entities | Using JSON in Caché | Caché & Ensemble 2018.1.4 – 2018.1.12
As previously mentioned, values are always stored and retrieved in ObjectScript format regardless of how they are represented in JSON syntax. The following examples demonstrate a few more facts that you should be aware of when using dot syntax. Creating dynamic object properties with dot syntax · This example uses a literal constructor and dot syntax to create dynamic object dynObj, containing properties named A, a, and C quote. In the literal string, all property names must be quoted.
🌐
JMP User Community
community.jmp.com › t5 › Discussions › How-to-build-a-dynamic-JSON-object › td-p › 208546
Solved: How to build a dynamic JSON object - JMP User Community
May 22, 2019 - Names default to here(1); MyAA = Associative Array(); MyAA["username"] = "bob" ; MyAA["address"] = "12345" ; myAA["aa"] = associative array(); myAA["aa"]["thing1"] = 14; myAA["aa"]["thing2"] = 28; show( As JSON Expr(myAA)); ... @vince_faller , As some of the parameters are not something I can share here, I sent it to you as a private message. You will see from the message that the hard-coded example and the As JSON Expr() have the same parameters , the values are dynamic, but it results in an error as I have shown earlier.
🌐
Incontact
help.incontact.com › Content › Studio › Guide › DynamicDataObjects.htm
Dynamic Data Objects - HELP CENTER
If you change value of one, it does not automatically change the other value. You can use a dynamic data object to parse JSON or XML. Define the dynamic data object and use the FROM command to specify the JSON or XML data with this syntax: ... You can specify a 'string' that contains JSON or ...
🌐
Blogger
datanrg.blogspot.com › 2021 › 03 › how-to-add-constant-value-to-dynamic.html
How to add a Constant value to Dynamic JSON array in Azure Data Factory
March 23, 2021 - And here is the GitHub location to this ADF pipeline with all the code within: https://github.com/NrgFly/Azure-DataFactory/blob/master/Samples/pipeline/pl_constant_dynamic_JSON.json ... The official Microsoft documentation states that JOIN function in ADF returns a string that has all the items from an array and has each character separated by a delimiter.