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.
Any help is appreciated.
@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
Ah.
I had a transcribing error going from picture to text.
If 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
}
]
And don't forget you can create JSON by just concatenating strings in the JSON format.
No Lists or Associative Arrays needed.