Ask Your Question
3

What is a way to add text to a JSON object when printing it?

asked 2022-10-05 11:00:00 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-06-07 04:00:00 +0000

bukephalos gravatar image

One way is to convert the JSON object to a string, add the desired text, and then convert it back to a JSON object. For example:

import json

my_dict = { "key": "value" }
my_str = json.dumps(my_dict)
my_str_with_text = "Additional text: " + my_str
my_object_with_text = json.loads(my_str_with_text)

print(my_object_with_text)

This will output:

{'Additional text': {'key': 'value'}}
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-10-05 11:00:00 +0000

Seen: 17 times

Last updated: Jun 07 '22