Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
JSON API / no braces
#1
Hello:

I'm new to Python. I'm using 3.5.3 on Windows.

Got a bit of a struggle with JSON where the data has no braces {} and only has brackets []. I'm unable to post the link (new to the forum, not a proven non-spammer). The data is from the US Census. The goal is to dump the extract onto a local text file. There are many of files I need to pull, so saving CSVs is not really an option.

Here is a sample of the data

[
  • [
    • "BIRTHS",
    • "YEAR",
    • "us"


  • ],


  • [
    • "3969976",
    • "2014",
    • "1"



  • ],


  • [
    • "3998730",
    • "2015",
    • "1"




  • ],


Based on the standard, it seems this is OK, if the data has no key.

The Python syntax below "bombs" when I assign it to the S variable using method "dump" for a string, so "dumps"


>>> import requests
>>> 
>>> response = requests.get("....census api call goes here with key....")
>>> import json


# bombs on this line below, everything above is OK
>>> s=json.dumps(response)


basically this comes back as is not JSON serializable ... from the standards, I understand why this is happening. I can't find a solution that will "serialize it."

Any thoughts would be appreciated. 

Thank you
Mariusz
Reply
#2
Requests has json decoder build in,this apply in an out of server.
Eg:
>>> import requests

>>> data = {"device_name": 'SuperPc', "type": "DevicePort", "page_size": 999}
>>> r = requests.post('http://httpbin.org/post', json=data)
>>> r.status_code
200
>>> r.json()
{'args': {},
'data': '{"device_name": "SuperPc", "page_size": 999, "type": "DevicePort"}',
'files': {},
'form': {},
'headers': {'Accept': '*/*',
            'Accept-Encoding': 'gzip, deflate',
            'Content-Length': '66',
            'Content-Type': 'application/json',
            'Host': 'httpbin.org',
            'User-Agent': 'python-requests/2.6.0 CPython/3.4.2 Windows/8'},
'json': {'device_name': 'SuperPc', 'page_size': 999, 'type': 'DevicePort'},
'origin': '95.143.193.193',
'url': 'http://httpbin.org/post'}

>>> r.json()['json']
{'device_name': 'SuperPc', 'page_size': 999, 'type': 'DevicePort'}
You dump the the json response from sever,call json() as show over(then it's a python dictionary).
So if if a want to dump(Serialize) data to disk it look like this.
import json

json_data = {'device_name': 'SuperPc', 'page_size': 999, 'type': 'DevicePort'}
with open("my_file.json", "w") as j_in:
   json.dump(json_data, j_in)
with open("my_file.json") as j_out:
   saved_data = json.load(j_out)
Test if will be same dictionary as dump(Serialize) to disk:
>>> saved_data
{'device_name': 'SuperPc', 'page_size': 999, 'type': 'DevicePort'}
>>> saved_data['page_size']
999
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to output set items without the curly braces ? jimthecanadian 3 2,912 May-11-2019, 07:02 AM
Last Post: perfringo
  search and replace dots inside curly braces kchinnam 1 2,642 May-01-2018, 06:53 PM
Last Post: Larz60+
  No curly braces in python? RedSkeleton007 9 9,154 Jul-28-2017, 11:01 AM
Last Post: tony1812
  Are braces rather than indentation ever wanted? michaelcollier 22 14,152 May-02-2017, 08:37 AM
Last Post: volcano63

Forum Jump:

User Panel Messages

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