0

I have the following JSON Format:

{
"insights": {
"data": [
  {
    "name": "page_impressions",
    "period": "day",
    "values": [
      {
        "value": 2,
        "end_time": "2022-05-19T07:00:00+0000"
      },
      {
        "value": 1,
        "end_time": "2022-05-20T07:00:00+0000"
      }
    ],
    "title": "Daily Total Impressions",
    "description": "Daily: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
    "id": "107188788663426/insights/page_impressions/day"
  },
  {
    "name": "page_impressions",
    "period": "week",
    "values": [
      {
        "value": 36,
        "end_time": "2022-05-19T07:00:00+0000"
      },
      {
        "value": 37,
        "end_time": "2022-05-20T07:00:00+0000"
      }
    ],
    "title": "Weekly Total Impressions",
    "description": "Weekly: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
    "id": "107188788663426/insights/page_impressions/week"
  },
  {
    "name": "page_impressions",
    "period": "days_28",
    "values": [
      {
        "value": 36,
        "end_time": "2022-05-19T07:00:00+0000"
      },
      {
        "value": 37,
        "end_time": "2022-05-20T07:00:00+0000"
      }
    ],
    "title": "28 Days Total Impressions",
    "description": "28 Days: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
    "id": "107188788663426/insights/page_impressions/days_28"
  }
],
"paging": {
  "previous": "https://graph.facebook.com/v13.0/107188788663426/insights?access_token=EAAQeCPGNpp4BAIkM5ulDmHe7V98aWaG5Mt4oQZA6x4YuSH1oo1dyucXuk4oPLireHPlqk2jZBuqNxfBC3yVAjNNCxIBv6RsIenz5gG43qZB4O11qp4COndKvkmZC6lEUqCLUz2SeiQ1aWDpe4YYEZC3j83ZAWRKxXAMiRONIuzhZCCDEuXJoGX7ePy6mnHEUURAZAp5tz3OUfz6VQstnlJ8b&pretty=0&metric=page_impressions&since=1652684400&until=1652857200",
  "next": "https://graph.facebook.com/v13.0/107188788663426/insights?access_token=EAAQeCPGNpp4BAIkM5ulDmHe7V98aWaG5Mt4oQZA6x4YuSH1oo1dyucXuk4oPLireHPlqk2jZBuqNxfBC3yVAjNNCxIBv6RsIenz5gG43qZB4O11qp4COndKvkmZC6lEUqCLUz2SeiQ1aWDpe4YYEZC3j83ZAWRKxXAMiRONIuzhZCCDEuXJoGX7ePy6mnHEUURAZAp5tz3OUfz6VQstnlJ8b&pretty=0&metric=page_impressions&since=1653030000&until=1653202800"
}
},
"id": "107188788663426"
}

I can iterate over the json data:

for data in test["insights"]["data"]:
print(data["title"])
for val in data["values"]:
    for key, value in val.items():
        print(f"The value: [ {value} ]")

But I can't figure out how can I POST it to my api. The table structure is as follow: id, total_impressions (day, weekly, 28days), values (changes depending on the day), and the date. Is there a way I can post data to the api in this format:

Page ID -- day -- 5(value) -- date

Page ID -- day -- 6(value) -- date

Page ID -- weekly -- 13(value) -- date

Page ID -- weekly -- 14(value) -- date

Page ID -- 28 -- 67(value) -- date

Page ID -- 28 -- 59(value) -- date

Any help would be appreciated. I'm getting this data from Facebook Graph API.

Fakhr Ali
  • 128
  • 2
  • 11
  • Does this answer your question? [How to POST JSON data with Python Requests?](https://stackoverflow.com/questions/9733638/how-to-post-json-data-with-python-requests) – tevemadar May 23 '22 at 07:53

1 Answers1

0

You do not need to format you JSON format to Python format before POSTing it to your API. It is the job of the backend/API to process the JSON data.

import requests

url = 'https://www.w3schools.com/python/demopage.php'
myobj = {
"insights": {
"data": [
  {
    "name": "page_impressions",
    "period": "day",
    "values": [
      {
        "value": 2,
        "end_time": "2022-05-19T07:00:00+0000"
      },
      {
        "value": 1,
        "end_time": "2022-05-20T07:00:00+0000"
      }
    ],
    "title": "Daily Total Impressions",
    "description": "Daily: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
    "id": "107188788663426/insights/page_impressions/day"
  },
  {
    "name": "page_impressions",
    "period": "week",
    "values": [
      {
        "value": 36,
        "end_time": "2022-05-19T07:00:00+0000"
      },
      {
        "value": 37,
        "end_time": "2022-05-20T07:00:00+0000"
      }
    ],
    "title": "Weekly Total Impressions",
    "description": "Weekly: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
    "id": "107188788663426/insights/page_impressions/week"
  },
  {
    "name": "page_impressions",
    "period": "days_28",
    "values": [
      {
        "value": 36,
        "end_time": "2022-05-19T07:00:00+0000"
      },
      {
        "value": 37,
        "end_time": "2022-05-20T07:00:00+0000"
      }
    ],
    "title": "28 Days Total Impressions",
    "description": "28 Days: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
    "id": "107188788663426/insights/page_impressions/days_28"
  }
],
"paging": {
  "previous": "https://graph.facebook.com/v13.0/107188788663426/insights?access_token=EAAQeCPGNpp4BAIkM5ulDmHe7V98aWaG5Mt4oQZA6x4YuSH1oo1dyucXuk4oPLireHPlqk2jZBuqNxfBC3yVAjNNCxIBv6RsIenz5gG43qZB4O11qp4COndKvkmZC6lEUqCLUz2SeiQ1aWDpe4YYEZC3j83ZAWRKxXAMiRONIuzhZCCDEuXJoGX7ePy6mnHEUURAZAp5tz3OUfz6VQstnlJ8b&pretty=0&metric=page_impressions&since=1652684400&until=1652857200",
  "next": "https://graph.facebook.com/v13.0/107188788663426/insights?access_token=EAAQeCPGNpp4BAIkM5ulDmHe7V98aWaG5Mt4oQZA6x4YuSH1oo1dyucXuk4oPLireHPlqk2jZBuqNxfBC3yVAjNNCxIBv6RsIenz5gG43qZB4O11qp4COndKvkmZC6lEUqCLUz2SeiQ1aWDpe4YYEZC3j83ZAWRKxXAMiRONIuzhZCCDEuXJoGX7ePy6mnHEUURAZAp5tz3OUfz6VQstnlJ8b&pretty=0&metric=page_impressions&since=1653030000&until=1653202800"
}
},
"id": "107188788663426"
}

x = requests.post(url, data = myobj)

print(x.text)
Akira Taguchi
  • 111
  • 1
  • 7
  • Sorry but this method isn't working for me. I get an "unsupported media type error" status 415. – Fakhr Ali May 23 '22 at 08:03
  • This is most likely an issue in your API. Try to debug it by sending smaller payloads(JSON) to it first and f.ex. print out the received payload from backend. – Akira Taguchi May 23 '22 at 08:36