@iluwa

Как конвертировать строку в json?

Делаю GET запрос к гитлабу:
url = 'https://gitlab.example/api/v4/projects/303/repository/commits/master/diff'
print(requests.get(url, headers={'Private-Token': 'blahblahblah'}).json())

он возвращает просто длинную строку со всеми "\n", "\n++" и т.д.:
spoiler
{'diff': '--- /dev/null\n+++ b/Uklon.Messaging.RabbitMq.Contracts/Uklon.Messaging.RabbitMq.Contracts/OrderProcessor/OrderArchiveCommand.cs\n@@ -0,0 +1,29 @@\n+\ufeffusing System;\n+using System.Diagnostics;\n+\n+namespace Uklon.IntegrationMessages.OrderProcessor\n+{\n+ [DebuggerDisplay("Id = {Id}; OrderId = {OrderId}; OccurredAt = {OccurredAt}")]\n+ public sealed class OrderArchiveCommand\n+ {\n+ private OrderArchiveCommand()\n+ {\n+ }\n+\n+ public OrderArchiveCommand(Guid id, Guid orderId, long occurredAt)\n+ {\n+ Id = id;\n+ OrderId = orderId;\n+ OccurredAt = occurredAt;\n+ }\n+\n+ public Guid Id { get; set; }\n+ public Guid OrderId { get; set; }\n+ public long OccurredAt { get; set; }\n+\n+ public override String ToString()\n+ {\n+ return $"{nameof(OrderArchiveCommand)}: id={OrderId};";\n+ }\n+ }\n+}\n\\ No newline at end of file\n', 'new_path': 'Uklon.Messaging.RabbitMq.Contracts/Uklon.Messaging.RabbitMq.Contracts/OrderProcessor/OrderArchiveCommand.cs', 'old_path': 'Uklon.Messaging.RabbitMq.Contracts/Uklon.Messaging.RabbitMq.Contracts/OrderProcessor/OrderArchiveCommand.cs', 'a_mode': '0', 'b_mode': '100644', 'new_file': True, 'renamed_file': False, 'deleted_file': False, 'too_large': None}, {'diff': '--- /dev/null\n+++ b/Uklon.Messaging.RabbitMq.Contracts/Uklon.Messaging.RabbitMq.Contracts/OrderProcessor/OrderCancelByTimeoutCommand.cs\n@@ -0,0 +1,29 @@\n+\ufeffusing System;\n+using System.Diagnostics;\n+\n+namespace Uklon.IntegrationMessages.OrderProcessor\n+{\n+ [DebuggerDisplay("Id = {Id}; OrderId = {OrderId}; OccurredAt = {OccurredAt}")]\n+ public sealed class OrderCancelByTimeoutCommand\n+ {\n+ private OrderCancelByTimeoutCommand()\n+ {\n+ }\n+\n+ public OrderCancelByTimeoutCommand(Guid id, Guid orderId, long occurredAt)\n+ {\n+ Id = id;\n+ OrderId = orderId;\n+ OccurredAt = occurredAt;\n+ }\n+\n+ public Guid Id { get; set; }\n+ public Guid OrderId { get; set; }\n+ public long OccurredAt { get; set; }\n+\n+ public override String ToString()\n+ {\n+ return $"{nameof(OrderCancelByTimeoutCommand)}: id={OrderId};";\n+ }\n+ }\n+}\n\\ No newline at end of file\n', 'new_path': 'Uklon.Messaging.RabbitMq.Contracts/Uklon.Messaging.RabbitMq.Contracts/OrderProcessor/OrderCancelByTimeoutCommand.cs', 'old_path': 'Uklon.Messaging.RabbitMq.Contracts/Uklon.Messaging.RabbitMq.Contracts/OrderProcessor/OrderCancelByTimeoutCommand.cs', 'a_mode': '0', 'b_mode': '100644', 'new_file': True, 'renamed_file': False, 'deleted_file': False, 'too_large': None}, {'diff': '--- /dev/null\n+++ b/Uklon.Messaging.RabbitMq.Contracts/Uklon.Messaging.RabbitMq.Contracts/OrderProcessor/OrderCompleteCommand.cs\n@@ -0,0 +1,29 @@\n+\ufeffusing System;\n+using System.Diagnostics;\n+\n+namespace Uklon.IntegrationMessages.OrderProcessor\n+{\n+ [DebuggerDisplay("Id = {Id}; OrderId = {OrderId}; OccurredAt = {OccurredAt}")]\n+ public sealed class OrderCompleteCommand\n+ {\n+ private OrderCompleteCommand()\n+ {\n+ }\n+\n+ public OrderCompleteCommand(Guid id, Guid orderId, long occurredAt)\n+ {\n+ Id = id;\n+ OrderId = orderId;\n+ OccurredAt = occurredAt;\n+ }\n+\n+ public Guid Id { get; set; }\n+ public Guid OrderId { get; set; }\n+ public long OccurredAt { get; set; }\n+\n+ public override String ToString()\n+ {\n+ return $"{nameof(OrderCompleteCommand)}: id={OrderId};";\n+ }\n+ }\n+}\n\\ No newline at end of file\n', 'new_path': 'Uklon.Messaging.RabbitMq.Contracts/Uklon.Messaging.RabbitMq.Contracts/OrderProcessor/OrderCompleteCommand.cs', 'old_path': 'Uklon.Messaging.RabbitMq.Contracts/Uklon.Messaging.RabbitMq.Contracts/OrderProcessor/OrderCompleteCommand.cs', 'a_mode': '0', 'b_mode': '100644', 'new_file': True, 'renamed_file': False, 'deleted_file': False, 'too_large': None}, {'diff': '--- /dev/null\n+++ b/Uklon.Messaging.RabbitMq.Contracts/Uklon.Messaging.RabbitMq.Contracts/OrderProcessor/OrderStartDeferredCommand.cs\n@@ -0,0 +1,29 @@\n+\ufeffusing System;\n+using System.Diagnostics;\n+\n+namespace Uklon.IntegrationMessages.OrderProcessor\n+{\n+ [DebuggerDisplay("Id = {Id}; OrderId = {OrderId}; OccurredAt = {OccurredAt}")]\n+ public sealed class OrderStartDeferredCommand\n+ {\n+ private OrderStartDeferredCommand()\n+ {\n+ }\n+\n+ public OrderStartDeferredCommand(Guid id, Guid orderId, long occurredAt)\n+ {\n+ Id = id;\n+ OrderId = orderId;\n+ OccurredAt = occurredAt;\n+ }\n+\n+ public Guid Id { get; set; }\n+ public Guid OrderId { get; set; }\n+ public long OccurredAt { get; set; }\n+\n+ public override String ToString()\n+ {\n+ return $"{nameof(OrderStartDeferredCommand)}: id={OrderId};";\n+ }\n+ }\n+}\n\\ No newline at end of file\n', 'new_path': 'Uklon.Messaging.RabbitMq.Contracts/Uklon.Messaging.RabbitMq.Contracts/OrderProcessor/OrderStartDeferredCommand.cs', 'old_path': 'Uklon.Messaging.RabbitMq.Contracts/Uklon.Messaging.RabbitMq.Contracts/OrderProcessor/OrderStartDeferredCommand.cs', 'a_mode': '0', 'b_mode': '100644', 'new_file': True, 'renamed_file': False, 'deleted_file': False, 'too_large': None}

пытался записывать в файл - то же самое.
как получить читабельный json?
  • Вопрос задан
  • 231 просмотр
Решения вопроса 1
Пригласить эксперта
Ответы на вопрос 1
darqsat
@darqsat
PM
Тебе нужно использовать не .json() а .text
- json() десериализует json и превращает его в dict, заменяя null на None, true/false на True/False так что бы питон мог с этим работать;
- text возвращает содержание респонза как строку. В твоем случае, если тебе пришел в содержании json то ты его там и получишь.

То что тебе сверху посоветовали это похоже на оверкил. Использовать еще один модуль, что бы серелиазовать в json когда он у тебя уже есть в response.text
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через TM ID
Похожие вопросы