-3
{
   "blogs": [
        {
            "header": "Welcome",
           "author": "Auriga",
           "team"  : "Webmaster",
           "date"  : ["2015", "12", "12"],
           "paragraphs" : [
           "Blah blah blah"
        ],
        "images": []
    }
],

Basically I want to make it it adds another header,author,team,date,paragraphs, and images in the same format.

Also I need to do it inside the "blogs" section.

Thanks!!

EDIT*: Also how can I post text next the header for example.

maxadorable
  • 1,290
  • 1
  • 10
  • 24
  • 2
    don't post code as an image. post the code in your post. – Joe Dec 12 '15 at 22:21
  • What is wrong with posting as a image? – maxadorable Dec 12 '15 at 22:22
  • http://stackoverflow.com/help/how-to-ask – msw Dec 12 '15 at 22:23
  • 2
    @Auriga - it requires a separate click (to a URL shortener I don't recognize, not gonna click it), makes the code unable to be copied/pasted for trying to run, and makes this post useless in the future if the image link is broken. Include everything you need in your question within the actual post. – Joe Dec 12 '15 at 22:23
  • 1
    Please take a look at [_How to ask_](http://stackoverflow.com/help/how-to-ask) and [_MCVE_](http://stackoverflow.com/help/mcve). – albert Dec 12 '15 at 22:23
  • So what did you try? Or did you just want us to write code for you? – msw Dec 12 '15 at 22:29
  • I tried using this [link](http://stackoverflow.com/questions/21035762/python-read-json-file-and-modify) but I cant figure out how to put it inside my blog section. – maxadorable Dec 12 '15 at 22:34

1 Answers1

0

Its important to write example code that highlights your problem but doesn't get bogged down in extraneous details. With the example, we have a better understanding of the problem and can write an answer without a lot of extra explanation.

In your case, the question is puzzling because you don't update JSON directly (its just a string) - you load it into python and update the python objects. There are lots of other questions such as where did this json come from but they are not central to the issue of updating.

I took the liberty of writing an example that I hope is right

import json

json_str = """{
   "blogs": [
        {
            "header": "Welcome",
           "author": "Auriga",
           "team"  : "Webmaster",
           "date"  : ["2015", "12", "12"],
           "paragraphs" : [
           "Blah blah blah"
        ],
        "images": []
    }
]}"""

data = json.loads(json_str)

newblog = {
    "header": "Welcome",
    "author": "Auriga",
    "team": "Webmaster",
    "date": ["2015", "12", "12"],
   "paragraphs" : [
      "Blah blah blah"
    ],
    "images": []
}

# THE PROBLEM: How do I add the new blog to blogs?
????

print(json.dumps(data, indent=4))

and the answer is simply data['blogs'].append(newblog)

import json

json_str = """{
   "blogs": [
        {
            "header": "Welcome",
           "author": "Auriga",
           "team"  : "Webmaster",
           "date"  : ["2015", "12", "12"],
           "paragraphs" : [
           "Blah blah blah"
        ],
        "images": []
    }
]}"""

data = json.loads(json_str)

newblog = {
    "header": "Welcome",
    "author": "Auriga",
    "team": "Webmaster",
    "date": ["2015", "12", "12"],
   "paragraphs" : [
      "Blah blah blah"
    ],
    "images": []
}

# THE PROBLEM: How do I add the new blog to blogs?
data['blogs'].append(newblog)

print(json.dumps(data, indent=4))
tdelaney
  • 73,364
  • 6
  • 83
  • 116