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*
#5
snippsat,

Like you said this is very basic Python that is difficult for me as a newbie. I agree. Could I ask a few things to help me understand how to use what's being brought into python a little better until I fully understand how to read and modify the existing code?

for instance;

print(record['date_filed'])
print(record['citation']['federal_cite_one'])

How would you assign the value of 'date_filed' to

pythonvariable_date_filed = print(record['date_filed']) # DEFINED #

pythonvariable_citation = print(record['citation']['federal_cite_one']) # DEFINED #

I don't think I am understanding correctly.

(Mar-24-2020, 01:03 AM)snippsat Wrote:
(Mar-23-2020, 11:15 PM)BrandonKastning Wrote: Are the above >>> (<--- Is that a Python console command line) and then you typed "record['date_filed'] and it outputs "'1754-09-01' ?
>>> is the interactive active shell that come with all Python version,i use a better one ptpython.
This is very basic knowledge about Python,that you may struggle with Wink
Running as file .py it would look like this,then need to use print().
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)

print(record['date_filed'])
print(record['citation']['federal_cite_one'])
Output:
1754-09-01 1 U.S. 1

(Mar-23-2020, 11:15 PM)BrandonKastning Wrote: Does pandas have df.to_csv() and df.to_mysql() ?
Both to_csv and to_sql

(Mar-23-2020, 11:15 PM)BrandonKastning Wrote: 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 .
Are there ways of using dataframes in python without using Pandas; or is that recommended for something like this?
No,it can make data manipulation easier depend on the task that shall do with the data.
Can just use the dictionary and eg write those chosen values to a database.
There are also easier DB like TinyDB or dataset.

Demo dataset which has a lot power as build on top of SQLAlchemy.
import requests
from pprint import pprint
import dataset

url = 'https://raw.githubusercontent.com/brianwc/bulk_scotus/master/1700s/1754/84581.json'
response = requests.get(url)
record = response.json()
#pprint(record)

print(record['date_filed'])
print(record['citation']['federal_cite_one'])

#--- DB
db = dataset.connect('sqlite:///court.db')
table = db['court_table']
table.insert(dict(record=record['date_filed'], case=100))
table.insert(dict(record=record['citation']['federal_cite_one'], case=25)
Test.
λ ptpython -i court.py
>>> table.find_one(case=25)
OrderedDict([('id', 2), ('record', '1 U.S. 1'), ('case', 25)])

# can also to do SQL queries 
>>> result = db.query('SELECT * FROM court_table;')
>>> for row in result:
...     print(row)
OrderedDict([('id', 1), ('record', '1754-09-01'), ('case', 100)])
OrderedDict([('id', 2), ('record', '1 U.S. 1'), ('case', 25)])

Basically how to take the dictionary that was once the .json file and turn it into a dictionary like the python program does using import requests.

How to assign a python variable to each record in the dictionary?

I want to learn this prior to passing it over to dataset to make it easier for me to understand.
“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 - Apr-19-2020, 05:18 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 256 Mar-28-2024, 05:16 PM
Last Post: deanhystad
Exclamation Json API JayPy 4 474 Mar-04-2024, 04:28 PM
Last Post: deanhystad
  json loads throwing error mpsameer 8 728 Jan-23-2024, 07:04 AM
Last Post: deanhystad
  Parsing large JSON josvink66 5 703 Jan-10-2024, 05:46 PM
Last Post: snippsat
  parse json field from csv file lebossejames 4 774 Nov-14-2023, 11:34 PM
Last Post: snippsat
  format json outputs ! evilcode1 3 1,766 Oct-29-2023, 01:30 PM
Last Post: omemoe277
  JSON Dump and JSON Load foxholenoob 8 1,144 Oct-12-2023, 07:21 AM
Last Post: foxholenoob
  TypeRoor reading json GreenLynx 3 892 May-16-2023, 01:47 PM
Last Post: buran
  Python Script to convert Json to CSV file chvsnarayana 8 2,570 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,166 Apr-06-2023, 11:15 AM
Last Post: AlphaInc

Forum Jump:

User Panel Messages

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