Python Forum

Full Version: append into json
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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);
(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!
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?