How to build a dynamic JSON objectAll,
I am wondering how to build a dynamic JSON object in JSL ?
request = New HTTP Request(
url( "http://httpbin.org/post" ),
Method( "POST" ),
JSON( "\[{"username":"bob","address":"12345"}]\" )
); Using the example from the scripting index, if I wantedt to vary the username from bob to say mark and address from 12345 to 12346 and I have 10 such calls, I would prefer building that as a dynamic object outside. I initially ventured down the path of building it as an Associative Array and then using the As JSON Expr() , however, that is resulting in an error. My approach in that case was:
MyAA = Associative Array();
MyAA["username"] = "bob" ;
MyAA["address"] = "12345" ;<BR /><BR />request = New HTTP Request(
url( "http://httpbin.org/post" ),
Method( "POST" ),
JSON( As Json Expr(MyAA))
);
Of course, this is not happening for this example, but the api I am trying to call on. However, when I pass hardcoded JSON as an input, the api call works as expected.

Any help is appreciated.
Re: How to build a dynamic JSON objectThat was for building it step by step, the complete "hardcoded" version would be just
monthlyFactors = { {
[ "month" => 1, "month_name" => "Jan", "albedo" => 0.2, "soiling_loss" => 2.0],
[ "month" => 2, "month_name" => "Feb", "albedo" => 0.2, "soiling_loss" => 2.0],
[ "month" => 3, "month_name" => "Mar", "albedo" => 0.2, "soiling_loss" => 2.0]
}};
je = As JSON Expr(monthlyFactors);
Show(je);
As a rule of thumb:
* JSON arrays map to JSL lists (not matrices or vectors, beware of the '[]' similarity);
* JSON dictionaries map to JSL associative arrays;
* Check for non-evaluated variable references;
* Check if data structure modifying calls actually change the target object (as opposed to returning a modified copy).
Re: How to build a dynamic JSON objectAnd don't forget you can create JSON by just concatenating strings in the JSON format.
No Lists or Associative Arrays needed.
Re: How to build a dynamic JSON objectIf I rewrite it like this using the chrome add in JSON Editor, I see a List of Associative Arrays
[
{
"month": 1,
"month_name": "Jan",
"albedo": 0.2,
"soiling_loss": 2
},
{
"month": 2,
"month_name": "Feb",
"albedo": 0.2,
"soiling_loss": 2
},
{
"month": 3,
"month_name": "Mar",
"albedo": 0.2,
"soiling_loss": 2
},
{
"month": 4,
"month_name": "Apr",
"albedo": 0.2,
"soiling_loss": 2
},
{
"month": 5,
"month_name": "May",
"albedo": 0.2,
"soiling_loss": 2
},
{
"month": 6,
"month_name": "Jun",
"albedo": 0.2,
"soiling_loss": 2
},
{
"month": 7,
"month_name": "Jul",
"albedo": 0.2,
"soiling_loss": 2
},
{
"month": 8,
"month_name": "Aug",
"albedo": 0.2,
"soiling_loss": 2
},
{
"month": 9,
"month_name": "Sep",
"albedo": 0.2,
"soiling_loss": 2
},
{
"month": 10,
"month_name": "Oct",
"albedo": 0.2,
"soiling_loss": 2
},
{
"month": 11,
"month_name": "Nov",
"albedo": 0.2,
"soiling_loss": 2
},
{
"month": 12,
"month_name": "Dec",
"albedo": 0.2,
"soiling_loss": 2
}
]

Re: How to build a dynamic JSON objectThis seems to work.
innerList = {};
InsertInto(innerList, [ "month" => 1, "month_name" => "Jan", "albedo" => 0.2, "soiling_loss" => 2.0]);
InsertInto(innerList, [ "month" => 2, "month_name" => "Feb", "albedo" => 0.2, "soiling_loss" => 2.0]);
InsertInto(innerList, [ "month" => 3, "month_name" => "Mar", "albedo" => 0.2, "soiling_loss" => 2.0]);
monthlyFactors = {innerList};
je = As JSON Expr(EvalList(monthlyFactors));
Show(je);
I think the real issue is knowing the right API calls to the JSL list and associative arrays data structures, and their behavior as far as evaluating variables, returning a copy vs a live reference, and so on. The fact that they are being used to build a JSON object is incidental.
Having said that, with the new support for REST APIs from JMP I agree that having a session with a more detailed, end-to-end example would be helpful. I will pass this sugestion alone to our doc team. Thanks for the suggestion!
Re: How to build a dynamic JSON objectAh.
I had a transcribing error going from picture to text.
Re: How to build a dynamic JSON objectDo you have the link to the API. I don't think you've listed valid JSON.
At least the JSON Editor in Chrome doesn't think so.
ThanksRe: How to build a dynamic JSON object@nascif_jmp and @bryan_boone ,
Thank you for your responses. Your responses helped me get somewhere, however I have a couple of questions:
1. Can you kindly provide an example of accepted JSON array format in JSL ? Is this accepted ?

2. Does the scripting index or scripting guide cover the portions on JSON array or how to define an empty JSON object as nasicf has kindly shown ?
a. If yes, I would like to read that
b. If no, I would request that this be added to the documentation, because I couldn't find it anywhere
Re: How to build a dynamic JSON objectThe JavaScript/Python syntax for lists ("[]") does not mean the same thing in JSL. In JSL '[]' denotes an empty matrix, which does not a have valid JSON representation, which is why you are getting the '?' operator in the result. That '?' operator in turn must be what is causing the remote error message you are getting as a response from your service.
You should use instead '{}' to indicate an empty list, or '[=>]' to indicate an empty associative array/dictionary.
It is always a good practice to capture intermediate results and examine them using show() to test your assumptions.
http://wiki.c2.com/?MakeItWorkMakeItRightMakeItFast
Re: How to build a dynamic JSON objectYou can build "dynamic" JSON but you'd have to add your sub-elements to the associative array after they have been created.
That's just the way JSL works.
This shows what I mean:
aa = [=>];
list = {};
aa["a jsl list"] = list;
Insert Into(list, "Hello World");
Insert Into(list, 2019);
show(list);
show(aa);
outputs:
list = {"Hello World", 2019};
aa = ["a jsl list" => {}];
While this:
aa = [=>];
list = {};
Insert Into(list, "Hello World");
Insert Into(list, 2019);
aa["a jsl list"] = list;
show(list);
show(aa);
Outputs:
list = {"Hello World", 2019};
aa = ["a jsl list" => {"Hello World", 2019}];
Re: How to build a dynamic JSON objectI have not followed this thread at all but saw a mention of the use of associative arrays and what appeared as confusion about the defauilt result. That is the only aspect to which I am replying.
See Help > Books > Scripting Guide and search for associative arrays in the Table of Contents:
Default Values
A default value determines the value of a key that does not exist in an associative array. If you
try to access a key that does not exist in an associative array, an error results. If you define a
default value for your associative array, accessing a key that does not exist results in the
following:
• adds the key to the associative array
• assigns the default value to the new key
• returns the new key’s (default) value instead of an error
If you construct an associative array from a list of strings without assigning values to the keys,
then the keys are assigned values of 1. The default value for the associative array is set to 0.
To set the default value:
cary = Associative Array();
cary << Set Default Value( "Cary, NC" );<BR />
To determine whether there is a default value set for an associative array, use the <
Default Value message.
cary << Get Default Value;
"Cary, NC"
If there is no default value, Empty() is returned.
Besides the Set Default Value message, a default value can be set in the literal constructor
using =>value without a key.
counts = ["a" => 10,
"b" => 3,
=> 0]; // default value of 0
counts["c"] += 1;
Show( counts );
counts = ["a" => 10, "b" => 3, "c" => 1, => 0];
In the first line, the default value is set to 0. In the second line, the key "c" does not exist in
counts. In the output, the key "c" is created with the default value of 0 and then incremented
by 1.
Note: If a key’s value is the default value, then the key is dropped because any key will return
the default value.
Re: How to build a dynamic JSON object@Mark_Bailey ,
When I use "[ ]" , that results in an error. 
Re: How to build a dynamic JSON objectYou can define any result that you want returned when referring to a key that does not have a value in a pair in an associative array.
Re: How to build a dynamic JSON objectCan I firstly recommend that you don't just copy a picture, but use the JSL code functionality so people can copy and paste your code?
I assume you're trying to give a vector to username?
I'm still not having it crash with httpbin, though it is still doing the weird operator thing. If I used a list instead of a matrix though it seems to do what I think you want.
Names default to here(1);
MyAA = Associative Array();
MyAA["username"] = {1, 2, 3};
MyAA["address"] = "12345";
request = New HTTP Request(
url( "http://httpbin.org/post" ),
Method( "POST" ),
JSON( AsJSONExpr(myAA) )
);
data = request << Send;
Does that help?
Re: How to build a dynamic JSON object@vince_faller,
So the issue is stemming from this:

If the associative array has a key with no value, then upon using the As JSON Expr(), it gets translated to this wierd operator(?) which is not something that can be parsed properly which in turn throws an error. In my case, I just dropped the key from my Associative Array and it worked, however I am curious to know what is the right way to pass such a parameter ?
Re: How to build a dynamic JSON object@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.
Re: How to build a dynamic JSON objectHave you confirmed that your AS JSON Expr() is the same as your hardcoded?
Building the JSON object seems to be working.
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));