I
I
iluwa2018-06-21 17:37:43
Python
iluwa, 2018-06-21 17:37:43

How to convert string to json?

I make a GET request to gitlab:

url = 'https://gitlab.example/api/v4/projects/303/repository/commits/master/diff'
print(requests.get(url, headers={'Private-Token': 'blahblahblah'}).json())

it returns just a long string with all "\n", "\n++" etc.:
spoiler
{'diff': '--- /dev/null\n+++ b/Uklon.Messaging.RabbitMq.Contracts/Uklon.Messaging.RabbitMq.Contracts/OrderProcessor/OrderArchiveCommand.cs\[email protected]@ -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\[email protected]@ -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\[email protected]@ -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\[email protected]@ -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}

tried to write to a file - the same thing.
how to get readable json?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Yarkov, 2018-06-21
@iluwa

https://pythonworld.ru/moduli/modul-json.html

T
Tim, 2018-06-21
@darqsat

You need to use .text instead of .json()
- json() deserializes json and turns it into a dict, replacing null with None, true/false with True/False so that python can work with it;
- text returns the content of the response as a string. In your case, if you received json in the content, then you will receive it there.
The fact that you were advised from above is like an overkill. Use another module to serialize to json when you already have it in response.text

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question