cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
uday_guntupalli
Level VIII

How to build a dynamic JSON object

All, 
    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" ;

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. 

 

image.png

Any help is appreciated. 

Best
Uday
1 ACCEPTED SOLUTION

Accepted Solutions
nascif_jmp
Level VI

Re: How to build a dynamic JSON object

The 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

View solution in original post

16 REPLIES 16
vince_faller
Super User (Alumni)

Re: How to build a dynamic JSON object

Have 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));
Vince Faller - Predictum
uday_guntupalli
Level VIII

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. 

Best
Uday
uday_guntupalli
Level VIII

Re: How to build a dynamic JSON object

@vince_faller

     So the issue is stemming from this: 


image.png

 

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 ? 

Best
Uday
vince_faller
Super User (Alumni)

Re: How to build a dynamic JSON object

Can 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?

Vince Faller - Predictum

Re: How to build a dynamic JSON object

You 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.

uday_guntupalli
Level VIII

Re: How to build a dynamic JSON object

@Mark_Bailey , 

  When I use "[ ]" , that results in an error. image.png

Best
Uday

Re: How to build a dynamic JSON object

I 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" );

 

To determine whether there is a default value set for an associative array, use the <<Get
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

You 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}];
nascif_jmp
Level VI

Re: How to build a dynamic JSON object

The 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