Python Forum
JSON -> CSV conversion help! *I think Nested JSON*
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
JSON -> CSV conversion help! *I think Nested JSON*
#3
(Mar-23-2020, 11:03 PM)snippsat Wrote: As it's on GitHub to get all.
git clone https://github.com/brianwc/bulk_scotus.git
Get single file click Raw then right click save as.

The normal way is when read json is that Python convert it a dictionary.
So a demo taking this file down with Requests,local on disk use json module.
>>> import requests
>>> from pprint import pprint

>>> url = 'https://raw.githubusercontent.com/brianwc/bulk_scotus/master/1700s/1754/84581.json'
>>> response = requests.get(url)
>>> record = response.json()
>>> pprint(record)
{'absolute_url': '/opinion/84581/anonymous/',
 'blocked': False,
 'citation': {'case_name': 'Anonymous',
              'docket_number': None,
              'document_uris': ['/api/rest/v2/document/84581/'],
              'federal_cite_one': '1 U.S. 1',
              'federal_cite_three': None,
              'federal_cite_two': None,
              'id': 51503,
              'lexis_cite': '',
              'neutral_cite': None,
              'resource_uri': '/api/rest/v2/citation/51503/',
              'scotus_early_cite': None,
              'specialty_cite_one': None,
              'state_cite_one': None,
              'state_cite_regional': None,
              'state_cite_three': None,
              'state_cite_two': None,
              'westlaw_cite': None},
 'citation_count': 14,
 'court': '/api/rest/v2/jurisdiction/scotus/',
 'date_blocked': None,
 'date_filed': '1754-09-01',
 'date_modified': '2014-12-21T01:23:36.691370',
 'docket': '/api/rest/v2/docket/515705/',
 'download_url': 'http://bulk.resource.org/courts.gov/c/US/1/1.US.1.html',
 'extracted_by_ocr': False,
 'html': '<p class="case_cite">1 U.S. 1</p>\n'
         '    <p class="case_cite">1 Dall. 1</p>\n'
         '    <p class="case_cite">1 L.Ed. 11</p>\n'
         '    <p class="parties">Anonymous.</p>\n'
         '    <p class="court">Supreme Court of Pennsylvania</p>\n'
         '    <p class="date">September Term, 1754.</p>\n'
         '    <p>1 U.S. 1</p>\n'
         '    <p>1 Dall. 1</p>\n'
         '    <p>1 L.Ed. 11</p>\n'
         '    <div class="num" id="p1">\n'
         '      <span class="num">1</span>\n'
         '      <p>Anonymous.</p>\n'
         '    </div>\n'
         '    <p>Supreme Court of Pennsylvania</p>\n'
         '    <div class="num" id="p2">\n'
         '      <span class="num">2</span>\n'
         '      <p>September Term, 1754.</p>\n'
         '    </div>\n'
         '    <div class="num" id="p3">\n'
         '      <span class="num">3</span>\n'
         '      <p class="indent">Adjudged by the Court, that the Statute of '
         'Frauds and Perjuries<a class="footnote" href="#fn-s" '
         'id="fn-s_ref">*</a> does not extend to this Province, though made '
         "before Mr. Penn's Charter: The Governor of New-York having exercised "
         'A Jurisdiction here, before the making that Statute, by Virtue of '
         'the Word Territories, in the Grant to the Duke of York, of New-York '
         'and New-Jersey.</p>\n'
         '    </div>\n'
         '    <div class="footnotes">\n'
         '      <div class="footnote" id="fn-s">\n'
         '        <a class="footnote" href="#fn-s_ref">*</a>\n'
         '        <p> 29.Car.2.c.3. This statute was supplied, however by an '
         'act of General Assembly passed the 12 Geo. 3. 31. 1 State Laws 462. '
         'and sec 2 P. Will. 75.</p>\n'
         '      </div>\n'
         '    </div>\n'
         '    ',
 'html_lawbox': '',
 'html_with_citations': '<p class="case_cite"><span class="citation '
                        'no-link"><span class="volume">1</span> <span '
                        'class="reporter">U.S.</span> <span '
                        'class="page">1</span></span></p>\n'
                        '    <p class="case_cite">1 Dall. 1</p>\n'
                        '    <p class="case_cite">1 L.Ed. 11</p>\n'
                        '    <p class="parties">Anonymous.</p>\n'
                        '    <p class="court">Supreme Court of '
                        'Pennsylvania</p>\n'
                        '    <p class="date">September Term, 1754.</p>\n'
                        '    <p><span class="citation no-link"><span '
                        'class="volume">1</span> <span '
                        'class="reporter">U.S.</span> <span '
                        'class="page">1</span></span></p>\n'
                        '    <p>1 Dall. 1</p>\n'
                        '    <p>1 L.Ed. 11</p>\n'
                        '    <div class="num" id="p1">\n'
                        '      <span class="num">1</span>\n'
                        '      <p>Anonymous.</p>\n'
                        '    </div>\n'
                        '    <p>Supreme Court of Pennsylvania</p>\n'
                        '    <div class="num" id="p2">\n'
                        '      <span class="num">2</span>\n'
                        '      <p>September Term, 1754.</p>\n'
                        '    </div>\n'
                        '    <div class="num" id="p3">\n'
                        '      <span class="num">3</span>\n'
                        '      <p class="indent">Adjudged by the Court, that '
                        'the Statute of Frauds and Perjuries<a '
                        'class="footnote" href="#fn-s" id="fn-s_ref">*</a> '
                        'does not extend to this Province, though made before '
                        "Mr. Penn's Charter: The Governor of New-York having "
                        'exercised A Jurisdiction here, before the making that '
                        'Statute, by Virtue of the Word Territories, in the '
                        'Grant to the Duke of York, of New-York and '
                        'New-Jersey.</p>\n'
                        '    </div>\n'
                        '    <div class="footnotes">\n'
                        '      <div class="footnote" id="fn-s">\n'
                        '        <a class="footnote" href="#fn-s_ref">*</a>\n'
                        '        <p> 29.Car.2.c.3. This statute was supplied, '
                        'however by an act of General Assembly passed the 12 '
                        'Geo. 3. 31. 1 State Laws 462. and sec 2 P. Will. '
                        '75.</p>\n'
                        '      </div>\n'
                        '    </div>\n'
                        '    ',
 'id': 84581,
 'judges': '',
 'local_path': None,
 'nature_of_suit': '',
 'plain_text': '',
 'precedential_status': 'Published',
 'resource_uri': '/api/rest/v2/document/84581/',
 'sha1': 'd01a5ea90493b2357922308f2456c5d1479737f4',
 'source': 'R',
 'supreme_court_db_id': None,
 'time_retrieved': '2010-04-28T08:59:25'}
record is now a dictionary,so can access data easy.
 >>> record['date_filed']
'1754-09-01'

>>> record['citation']['federal_cite_one']
'1 U.S. 1'
Looking at data so do dictionary make more sense to work with this data than csv would do.
Can also take into Pandas,if need to do more manipulation of data.
Once inside a DataFrame then can also output easy to csv with df.to_csv().

snippsat,

I really like the idea of making the raw github .json file a python dictionary.

>>> record['date_filed']
'1754-09-01'

>>> record['citation']['federal_cite_one']
'1 U.S. 1'

Are the above >>> (<--- Is that a Python console command line) and then you typed "record['date_filed'] and it outputs "'1754-09-01' ?

If I am understanding correctly. The dictionary variables such as record['date_filed'] & all the rest would need to be sent to a data frame within python and then from that dataframe could send it to .csv using function "df.to_csv()". This same process could work from dataframe to MySQL/MariaDB?

Is Pandas a must? Or moving the data from one format to another sufficient without using Pandas. I notice you say it's useful for those who would require more data manipulation.

Are there ways of using dataframes in python without using Pandas; or is that recommended for something like this?

Does pandas have df.to_csv() and df.to_mysql() ?

Thank you a bunch! Giving me a bit of hope on figuring this out one day!
“And one of the elders saith unto me, Weep not: behold, the Lion of the tribe of Juda, the Root of David, hath prevailed to open the book,...” - Revelation 5:5 (KJV)

“And oppress not the widow, nor the fatherless, the stranger, nor the poor; and ...” - Zechariah 7:10 (KJV)

#LetHISPeopleGo

Reply


Messages In This Thread
RE: JSON -> CSV conversion help! *I think Nested JSON* - by BrandonKastning - Mar-23-2020, 11:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to generating multiple json files using python script dzgn989 4 254 May-10-2024, 03:09 PM
Last Post: deanhystad
  encrypt data in json file help jacksfrustration 1 314 Mar-28-2024, 05:16 PM
Last Post: deanhystad
Exclamation Json API JayPy 4 510 Mar-04-2024, 04:28 PM
Last Post: deanhystad
  json loads throwing error mpsameer 8 811 Jan-23-2024, 07:04 AM
Last Post: deanhystad
  Parsing large JSON josvink66 5 735 Jan-10-2024, 05:46 PM
Last Post: snippsat
  parse json field from csv file lebossejames 4 813 Nov-14-2023, 11:34 PM
Last Post: snippsat
  format json outputs ! evilcode1 3 1,789 Oct-29-2023, 01:30 PM
Last Post: omemoe277
  JSON Dump and JSON Load foxholenoob 8 1,238 Oct-12-2023, 07:21 AM
Last Post: foxholenoob
  TypeRoor reading json GreenLynx 3 927 May-16-2023, 01:47 PM
Last Post: buran
  Python Script to convert Json to CSV file chvsnarayana 8 2,629 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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