How to handle double quotes in error response payload from backend

Hi,

I am getting error response from backend with double quotes in error message. Backend error response from is different from the format i want to return to the consumer. In javascript I am extracting the backend error response in a variable and raising a raise fault from target fault rule. In raise fault i am setting the right format of error payload. But in apigee response i am getting invalid json as the error message has double quotes.

{ "code":"400", "message" : "Internal Error", "developerMessage": "Input error for "tripOptions". Not allowed." }.

How do i encode this double quote? I tried with .replace('/"/g', '\\') in javascript while extracting the error message. It didn't work.

Thanks,

Krish

Solved Solved
0 2 2,546
1 ACCEPTED SOLUTION

I'm not clear on the problem.

Are you saying that the backend is returning invalid JSON?

In that case, Google has a really good library for cleaning up documents that are "almost JSON" - (I cannot find the link just now, but will get it later)

If YOU are creating the JSON and wondering how to escape the quotes properly....I think your .replace() idea should work.

You wrote a description of what you are doing, but you didn't show real code and you didn't show the sequence of policies. I suspect that the problem is somehow related to your implementation, not to the approach.

So post more details and maybe one of us here can help.

View solution in original post

2 REPLIES 2

I'm not clear on the problem.

Are you saying that the backend is returning invalid JSON?

In that case, Google has a really good library for cleaning up documents that are "almost JSON" - (I cannot find the link just now, but will get it later)

If YOU are creating the JSON and wondering how to escape the quotes properly....I think your .replace() idea should work.

You wrote a description of what you are doing, but you didn't show real code and you didn't show the sequence of policies. I suspect that the problem is somehow related to your implementation, not to the approach.

So post more details and maybe one of us here can help.

Hi @Dino i got it worked out. Javacript would be

errorMessage.replace(/"/g, '\\"')

Thanks for you reply!