Json error

The following code throws an invalid JSON format error. Works in ColdFusion.

<cffunction name="getupsRate" access="remote" returntype="any" returnformat="JSON">
	<cfargument name="jsonObject" type="any" required="true">
		
	<cfset var dataObject = DeserializeJSON(Arguments.jsonObject)/>			

	<cfhttp method="post" url="https://onlinetools.ups.com/api/rating/v2205/Shop" result="upsRate">	
		<cfhttpparam name="Content-Type" type="header" value="application/json">
		<cfhttpparam name="Authorization" type="header" value="#dataObject.Bearer#">
		<cfhttpparam name="data" type="body" value="#dataObject.data#">
	</cfhttp>

	<cfset addChangeLog(Session.employeePK, 'Estimator', 'UPS', 0, 'Search')/>

	<cfreturn DeserializeJSON(upsRate.fileContent)/>
</cffunction>

First, which of those lines does it fail on? There are 2 deserializejson calls.

Also, since we can’t “just run it” (not having the input jsonobject arg value), you could help us (and perhaps help yourself) by dumping the args passed into the deserialize. I realize one might presume the problem is with the “output” but its work is of course based on the input. :slight_smile:

And if nothing about them seems obviously wrong, do a dump of them in cf for comparison? How do they differ, if at all?

1 Like

It comes back into Javascript and fails on the parseJSON function. Again, works in CF.

Thanks!

So you’re saying that the caller to the method is a js call from a browser? Ok.

So first, what ARE you getting as a response in that call from the browser. You can get that info via browser dev tools or even a simple js alert. You might see an error in what’s being returned (that’s NOT json).

Again, though, consider debugging things in cfml. I realize you can’t return a cfml dump to a js client expecting json, but you could dump to a file. Or you could call the method from your browser or a cfhttp, passing in the needed arg value. Even THAT may not be what your code expects. Question everything. :slight_smile:

Perhaps you thought there was some “known bug” (or known difference in Lucee vs ACF) where someone might just offer you “the solution”, negating such work. I’m not aware of any, and unless someone chimes in with one, the techniques above should get you far more clarity about whatever is not working.

Expecting JSON, when called using ColdFusion, the parseJSON works, when the exact same code is called in Lucee and passed to client via AJAX it fails.

Perhaps someone else will have the answer you seek.

Hello Charlie,
We are calling UPS with the code I showed you. It is returning JSON. The JSON is passed back to an AJAX call. When it comes back, I then parse the JSON into an Object. This works with no issues using CF server. When the code I sent you runs in Lucee, the JSON passed back to ther AJAX call is throwing an error. The best way to test this is to get an Account with UPS to hit their API. It’s free! Then call the function I sent you and send it back to AJAX and call parseJSON. Again, it works everytime in CF, but throws an error everytime it is executed with the Lucee server.

Thanks,

William

What I’m missing: Have you setup the content-type header? Could you check those headers? Also, make sure to reset the buffer to avoid additional whitespace around your json output:

cfcontent( 
    reset="true",
,   type="application/json"
);

Would also be nice to see the front end code that’s consuming your JSON.

1 Like

Hello Andreas,
I have done this a 1000 times. I am making and Ajax call to the backend code(which you have seen), the cfHTTP is making a call to UPS API for shipping costs. It returns data in JSON format. When the call is finished my AJAX success: function gets the data and does the following;

var responseObject = new Object();
responseObject = $.parseJSON(JSON.stringify(response));

This code works fine on ColdFusion, but on Lucee gives me an invalid JSON error.

Here is the JSON error on Lucee. It does not throw an error when executing the SAME code on ColdFusion.

VM84:1 Uncaught SyntaxError: “[object Object]” is not valid JSON
at Function.parse [as parseJSON] ()
at Object.success (:30:38)
at fire (jquery.js:3317:31)
at Object.fireWith [as resolveWith] (jquery.js:3447:7)
at done (jquery.js:9272:14)
at XMLHttpRequest. (jquery.js:9514:9)

I just ran on Lucee and got the above message. Ran on Coldfusion received no message and the application worked. Usde MS Edge for both.

Seems like something being returned from UPS, Lucee does not like or the Deserialization function is doing something??

I hope this helps.

Have you tried validating that JSON somewhere outside Lucee, e.g. some online JSON validator? Because the JS error says that isn’t a valid JSON, i’d like to see where it’s breaking syntatically.

Andreas,

Again, this works with no issue from Coldfusion. Here is the JSON on ColdFusion Server

image.png

William, I’m not aware of any “code you sent me”. Perhaps you just meant the original code above. Let’s be clear about that, so no one thinks you’d sent more to me alone, or that I’m not responding to that.

And while Andreas is trying to help now, if you both run out of ideas, there are still the several debugging suggestions I originally made, which you’ve not responded to. I accepted back then that you were preferring to just “get a solution” without that effort. It really would be a trivial effort, so please do reconsider it if you guys don’t solve it.

Sending working console output of what the CF Server is sending won’t help much to see what is happenijg with Lucee. Just re-asking again:

  • have you validated the JSON object elsewhere (to see where exactly it is syntatically breaking)?
  • Are the content type headers being set correctly on server response?

That code would not “[work] fine on ColdFusion”, as it’s JavaScript code. It doesn’t run on the CF server, it runs in the client browser. So I think you could perhaps benefit from getting clear in your head - and then clarify for us - what code is running where and when, and what the inputs and outputs are.

It also seems a curious thing to do: response is an object… you are then converting it to JSON… and then you are… converting it back into an object with $.parseJSON. What’s that all about?

Also this:

VM84:1 Uncaught SyntaxError: “[object Object]” is not valid JSON

Again… that’s a JS error. Nothing to do with CFML processing.

And what it’s telling you is pretty clear… you are trying to use the string [object Object] where you need JSON. Stop doing that. It won’t work :wink:

Now [object Object] is a red flag here. This is what one gets if one converts an object to a string, eg:

> o.toString()
<- '[object Object]'

And example of forcing this might be {} + "": JS needs both operands to be the same type here. It can’t coerce a string to be an object, but it can coerce the object to be a string (as per above).

If this is happening, you have a logic error in your code.

Finally there is simply no point in continually repeating “this works fine in CF”. If this was a discussion about running code on CF, that would be relevant. This is a Lucee forum, and I presume you want the code to work on Lucee. So let’s… well… check yer attitude slightly, if I’m to be completely honest… and focus on what it is about your code that is not perhaps 100% Lucee compatible. The way to start with this is to stop being defensive, and respond to the ppl trying to help you by answering their questions (like… Charlie’s questions, for one).


Moving on…

Here are my questions:

  1. what does Arguments.jsonObject contain? I presume it’s a string containing JSON. Exactly what JSON string? Also, as it’s a string, you might wanna change the type-check on that parameter to be "string". It has to be a string, not “any”.

  2. What’s the response from https://onlinetools.ups.com, including both body and status?

  3. What’s the JS code that’s making the HTTP req to getupsRate. The whole lot, including the HTTP req, and its success/fail handlers.

  4. What is the response and status code from that request?

Reminder: we don’t need to know that this works in CF, and how many times you’ve made it work in CF before. We are currently focusing on getting it working on Lucee the first time, and your objective here is to dispense with the rhetoric and just help us to help you. By answering our questions.

Cheers.

2 Likes

Charlie,

You seem to be worried about what people are thinking about you or if you are responding. I am just trying to show Lucee developers an issue that came up while using the Lucee server that is not present in ColdFusion. I am sure I can figure out a workaround to the problem.
Don’t worry about it anymore!

Have a good day!

$.parseJSON convert a string to a object.
You can’t pass to Lucee (to CF too) a js object.

You can see here:

There must be something else wrong, but it doesn’t look like it’s Lucee.

OK, Thanks!

I am going to suggest that maybe this line

Might have an issue, that is throwing an error and putting off your application. Maybe just comment it out, as the rest seems to be simple calls to HTTP and JSONSerialization.

hope that helps.

William, I honestly don’t see what made you “go there”, but let’s see if the others get you sorted, yes.

1 Like

Hello Mark,

All that line of code does is call a SQL statement that does an insert.
I was getting the error before I implemented this code.

Thanks though!