简体   繁体   English

非标准JSON和Azure逻辑应用程序

[英]Non-standard JSON and Azure Logic Apps

I have an API that produces JSON like this: 我有一个生成JSON的API,如下所示:

)]}',

{
    //JSON DATA
}

The //JSON DATA is valid JSON, but the )]}', up top is not. //JSON DATA是有效的JSON,但是)]}',顶部不是。

When I try to GET this data via a Logic App, I get: 当我尝试通过逻辑应用程序获取此数据时,我得到:

BadRequest. Http request failed: the content was not a valid JSON.

So, a few related questions: 那么,一些相关的问题:

1) Can I tell the logic app to return the invalid JSON anyway? 1)我可以告诉逻辑应用程序返回无效的JSON吗?

2) How can debug the issue better? 2)如何更好地调试问题? I happen to know that the response is invalid, but what if I didn't? 我碰巧知道答案是无效的,但如果我不知道怎么办? Can I see the raw data somewhere? 我可以在某处看到原始数据吗?

3) This is all done via the Azure web portal. 3)这一切都是通过Azure门户网站完成的。 Are there better tools? 有更好的工具吗? Visual Studio? 视觉工作室?

I should also mention that if I call a route on the same API that returns XML instead of JSON, then the Logic App works fine. 我还要提一下,如果我在返回XML而不是JSON的同一API上调用路由,那么Logic App工作正常。 So it definitely doesn't like the JSON response in particular. 所以它绝对不喜欢JSON响应。

Thanks! 谢谢!

First of all, please do not post three questions as a single question . 首先,请不要将三个问题作为一个问题发布


Question 1). 问题1)。 The best thing you can do is make the API return a valid JSON object. 您可以做的最好的事情是让API返回一个有效的JSON对象。 This is good for million reasons. 这有利于百万个原因。 Here're a few: 这里有几个:

  • it's pretty much a standard (either valid JSON or XML -- yeah, old school way); 它几乎是一个标准(有效的JSON或XML - 是的,老派的方式);
  • therefore, no users of this API (including you) will need to struggle and guess what's going on and why; 因此,此API的任何用户(包括您)都不需要努力猜测正在发生的事情以及原因;
  • your Logic App's step will just work without adding extra complexity; 您的Logic App的步骤将不增加额外复杂性的情况下正常运行;
  • you will make this world and your karma better. 你将使这个世界和你的业力变得更好。

If API-side changes are not within your reach, I don't think you can do much. 如果API方面的更改不在您的范围内,我认为您不会做太多。 If you're lucky and the HTTP action is successful (Status Code 2xx), you can try to use a Query Action with a function that truncates the first characters. 如果您很幸运并且HTTP操作成功(状态代码2xx),您可以尝试使用具有截断第一个字符的函数的查询操作 It will look something like this (I don't know the exact syntax): @Substring(body('myHttpGet'), 4, length(body('myHttpGet')) - 4) where myHttpGet is the id of the Http Get action. 它看起来像这样 (我不知道确切的语法): @Substring(body('myHttpGet'), 4, length(body('myHttpGet')) - 4)其中myHttpGet是Http Get的id行动。

However, once again, if possible, I strongly recommend fixing up the API which is the root cause of the problem, instead of dealing with garbage response after that. 但是,如果可能的话,我再次强烈建议修复问题的根本原因,而不是在此之后处理垃圾响应。

UPDATE Another thing you can do is wrap the dirty API. 更新您可以做的另一件事是包装脏API。 For example, you could create a trivial Azure Function that invokes the API you don't directly control, and sanitizes the response for you consumption requirements. 例如,您可以创建一个简单的Azure函数来调用您不直接控制的API,并根据您的消费需求清理响应。 This Azure Function function should be easy to call from the Logic App. 应该可以从Logic App轻松调用此Azure功能。 It costs almost nothing (unless we're talking millions of requests/month). 它的成本几乎为零(除非我们每个月都在谈论数百万个请求)。 The only drawback here is the increasing latency, which may be not an issue at all -- test it and see whether it adds less than 100ms or so... Oh, and don't forget to file a ticket with the API owner, they make our world a bad place! 这里唯一的缺点是增加延迟,这可能根本不是问题 - 测试它,看它是否增加不到100ms左右......哦,不要忘记向API所有者提交一张票,他们让我们的世界变得糟糕!


Question 2) In Azure Logic App web UI you can Look into the execution details and the error will definitely be there. 问题2)在Azure Logic App Web UI中,您可以查看执行细节,肯定会出现错误。

在此输入图像描述

在此输入图像描述

在此输入图像描述


Question 3) You're asking for a tool recommendation which is by definition a highly subjective thing and is off-topic on StackOverflow. 问题3)你要求提供一个工具推荐,根据定义,它是一个非常主观的东西,并且在StackOverflow上是偏离主题的。

TL/DR: The other app is not producing valid JSON. TL / DR: 另一个应用程序没有生成有效的JSON。

Meaning, this is not a problem for you to solve. 意思是,这不是你要解决的问题。 The other app has to return valid JSON if the owner claims it should. 如果所有者声称它应该是另一个应用程序必须返回有效的JSON。

If they cannot or will not produce valid JSON, then the first thing you need to do is inform your management that you will have to spend a lot of extra time accommodating their non-standard format. 如果他们不能或不会生成有效的JSON,那么您需要做的第一件事就是通知您的管理层,您将不得不花费大量额外的时间来适应他们的非标准格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM