Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
append into json
#1
Hi people.

I have this json:
{
	"blocks": [
		{
			"type": "context",
			"elements": [

			]
		}
	]
}
I need to append into tag "Elements" this:
{
  "type": "mrkdwn",
  "text": "My text"
}
The result should be this:
{
	"blocks": [
		{
			"type": "context",
			"elements": [
				{
					"type": "mrkdwn",
					"text": "My text"
				}
			]
		}
	]
}
How I should append into the tag "Element" ?

Thanks
Reply
#2
Try to implement the following algorithm:

1) parse json-string to Python dictionary (use json module to do this);
2) modify the dictionary you got at #1;
3) convert the dictionary to json-string (json module);
Reply
#3
(Mar-09-2020, 01:09 AM)scidam Wrote: Try to implement the following algorithm:

1) parse json-string to Python dictionary (use json module to do this);
2) modify the dictionary you got at #1;
3) convert the dictionary to json-string (json module);

Hi @scidam, thank for your response.
But how I cand append? Could you help me with an example ? I should append into "Element" tag
Thank in advance!
Reply
#4
This is minimal example.

In [1]: import json

In [2]: ds = """
   ...: {"tag": [1, 2, 3]}
   ...: """

In [3]: js = json.loads(ds)

In [4]: js['tag'].append(4)

In [5]: json.dumps(js)
Out[5]: '{"tag": [1, 2, 3, 4]}'
Could you show, what did you try so far?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  .json overwrites, rather than append?? Clives 12 4,596 Jun-07-2021, 04:13 PM
Last Post: buran
  Append JSON's and write to file faqsap 4 2,816 May-15-2020, 04:20 PM
Last Post: faqsap
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 2,523 Apr-22-2020, 01:01 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020