cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
caseofmondays
Level II

JSON Unterminated String Error when Submitting HTTP Request

Hello,

 

I have an online application that I can request data extractions from by submitting an HTTP request with the appropriate JSON fields.

 

One field (field2) within my JSON payload is a list type and can contain one or more items. The issue that I am facing is that my JMP script functions only if this list has a single item and not if it has two or more.

 

The error that I receive is:

"error":"com.google.gson.stream.MalformedJsonException: Unterminated string

 

What confuses me is that if I make the same HTTP request with the same JSON payload using Python's request library, the request goes through without an issue.

 

It seems like it may have to do with how JMP is formatting the JSON before it sends the request out.

 

Any ideas?

 

Below is a simplified version of my code for reference.

 

url = "http://www.example_url.com/request";
my_username = "username";
timeout_minutes = 2;

var_field1 = "\!"value1\!"";
var_field2 = "\!"value2a\!""; //works

//var_field2 = "\!"value2a\!", \!"value2b\!""; //does not work
var_field3 = "\!"value3\!""; payloadJSON = "{\!"test\!":{\!"field1\!":" || var_field1 || ",\!"field2\!":[" || var_field2 || "],\!"field3\!":[" || var_field3 || "]}}"; request = New HTTP Request( URL( url ), Method( "POST" ), Timeout(timeout_minutes*60), Headers ( { "header_field1": "header_value1", "header_field2": "header_value2" } ), Username(my_username), JSON(payloadJSON) ); data = request << Send();

 

 

1 REPLY 1

Re: JSON Unterminated String Error when Submitting HTTP Request

Not knowing the values you are using in value1, value2a, value2b, value3, I cannot definitively say what the problem is. Some characters in JSON have to be escaped (like \n, \t, ", ', and so on).

If possible, you can use:

Write(payloadJSON || "\!n");

after you have concatenated all the values for payloadJSON and see if valid JSON is produced by copy/paste into a JSON Parser like Chrome's JSON Editor Online plugin.

 

Unless it's very simple JSON, I typically use JSL Associative Arrays and use AsJSONExpr to form the JSON. It does the escaping for me. (The JSON message will also take an Associative Array)

 

In your case (the one the doesn't work) I'd write it like this to make sure all the quoting is handled for JSON

var_field1 = "\!"value1\!"";
var_field2 = {"\!"value2a\!", \!"value2b\!""};
var_field3 = "\!"value3\!"";


fields = [=>];
fields["field1"] = var_field1;
fields["field2"] = var_field2;
fields["field3"] = var_field3;


payloadJSON = [=>];
payloadJSON["test"] = fields;
payloadJSON = asJSONExpr(payloadJSON);

Write(payloadJSON || "\!n");