How to assign variables using JSON data in python - python

I wrote a python code using MySQL data, but then I decided to use JSON as a "database" rather than MySQL.
This is MySQL code :
mydb = mysql.connector.connect(host="localhost", user="nn", passwd="passpass")
mycursor = mydb.cursor()
event_fabricant = input('Inscrivez le nom de la compagnie : ')
mycursor.execute("""SELECT name_company,inspecteur1, inspecteur2, inspecteur3, ville, email FROM listedatabase.entreprises_inspecteurs WHERE name_company = %s""", (event_fabricant,))
data = mycursor.fetchall()
if data:
row = data[0]
event_location = row[4]
event_email = row [5]
How do I assign data like I did with MySQL but with JSON?
This is a sample of my JSON data, and below what I did so far.
JSON SAMPLE :
[
{
"id": 1,
"name_company": "Acier Michel",
"inspecteur1": "Hou, L",
"inspecteur2": "Caana, C",
"inspecteur3": "Luc, C",
"type": "Water",
"location": "Laval"
},
{
"id": 2,
"name_company": "Aciers ABC Inc.",
"inspecteur1": "Vali, M",
"inspecteur2": "Alemane, K",
"inspecteur3": "laszik, M",
"type": "NA",
"location": "St-Joseph de Sorel"
}
]
This is what I did so far but it's not exactly what i want :
import json
database = "convertcsv.json"
data = json.loads(open(database).read())
name_company = input("type company name: ")
for item in data:
if item['nom_entreprise'] == name_company:
print(item['inspecteur1'])
else:
print("Not Found")
What I need instead is to be able to assign to variable1 the inspecteur1 name.

If you want to assign the data just use: variable1 = item["inspecteur1"].
One issue with your JSON code above is that it will print Not Found for every record that does NOT match which I don't think it's what you want. Try:
found = False
for item in data:
if item['nom_entreprise'] == name_company:
print(item['inspecteur1'])
found = True
if not found:
print("Not Found")
If you feel like MySQL is too complex for your needs, may I suggest SQLite? It's supported out of the box in Python, there's no server process (just a file) and you get all the database features that JSON does not provide by itself.

Related

Create a nested data dictionary in Python

I have the data as below
{
"employeealias": "101613177",
"firstname": "Lion",
"lastname": "King",
"date": "2022-04-21",
"type": "Thoughtful Intake",
"subject": "Email: From You Success Coach"
}
{
"employeealias": "101613177",
"firstname": "Lion",
"lastname": "King",
"date": "2022-04-21",
"type": null,
"subject": "Call- CDL options & career assessment"
}
I need to create a dictionary like the below:
You have to create new dictionary with list and use for-loop to check if exists employeealias, firstname, lastname to add other information to sublist. If item doesn't exist then you have to create new item with employeealias, firstname, lastname and other information.
data = [
{"employeealias":"101613177","firstname":"Lion","lastname":"King","date":"2022-04-21","type":"Thoughtful Intake","subject":"Email: From You Success Coach"},
{"employeealias":"101613177","firstname":"Lion","lastname":"King","date":"2022-04-21","type":"null","subject":"Call- CDL options & career assessment"},
]
result = {'interactions': []}
for row in data:
found = False
for item in result['interactions']:
if (row["employeealias"] == item["employeealias"]
and row["firstname"] == item["firstname"]
and row["lastname"] == item["lastname"]):
item["activity"].append({
"date": row["date"],
"subject": row["subject"],
"type": row["type"],
})
found = True
break
if not found:
result['interactions'].append({
"employeealias": row["employeealias"],
"firstname": row["firstname"],
"lastname": row["lastname"],
"activity": [{
"date": row["date"],
"subject": row["subject"],
"type": row["type"],
}]
})
print(result)
EDIT:
You read lines as normal text but you have to convert text to dictonary using module json
import json
data = []
with open("/Users/Downloads/amazon_activity_feed_0005_part_00.json") as a_file:
for line in a_file:
line = line.strip()
dictionary = json.loads(line)
data.append(dictionary)
print(data)
You can create a nested dictionary inside Python like this:
student = {name : "Suman", Age = 20, gender: "male",{class : 11, roll no: 12}}

How to loop over data in JSON?

I had a MySQL database stored that way: Company_name, employee1, employee2, employee3.
When I input a company name, the code look for the company name in my database, then loop over employee1, employee2, and employee3 to check if one of them is free in my calendar.
This was my code to check for the employees :
for i in range(3):
employee = row[i+1]
How do I do translate this loop so it can read a JSON structure?
Example of my structure:
[
{
"id": 1,
"name_company": "Acier Michel",
"inspecteur1": "Hou, L",
"inspecteur2": "Caana, C",
"inspecteur3": "Luc, C",
"type": "Water",
"location": "Laval"
},
{
"id": 2,
"name_company": "Aciers ABC Inc.",
"inspecteur1": "Vali, M",
"inspecteur2": "Alemane, K",
"inspecteur3": "laszik, M",
"type": "NA",
"location": "St-Joseph de Sorel"
}
]
I want to be able to iterate through inspecteur1, inspecteur2 and inspecteur 3.
First translate the json to python object with
import json
userList = json.loads(yourJsonString)
Then iterate on the list
for user in userList:
print(user)
The data is a list of dictionaries
Use pandas
This assumes your list of dictionaries is in a file
import pandas as pd
import json
from pathlib import Path
# path to file
p = Path(r'c:\path_to_file\test.json')
# read the file
with p.open('r', encoding='utf-8') as f:
data = json.loads(f.read())
# load into pandas
df = pd.DataFrame(data)
print(df)
id name_company inspecteur1 inspecteur2 inspecteur3 type location
1 Acier Michel Hou, L Caana, C Luc, C Water Laval
2 Aciers ABC Inc. Vali, M Alemane, K laszik, M NA St-Joseph de Sorel
# search datafram
search = df[['inspecteur1', 'inspecteur2', 'inspecteur3']][df.name_company == 'Aciers ABC Inc.']
print(search)
inspecteur1 inspecteur2 inspecteur3
Vali, M Alemane, K laszik, M
Note addressing comment:
With search you have access to the desired values of inspecteur1-3
search.values returns an numpy array, which can be iterated through.
There is not enough information in the question to offer a more comprehensive solution.
for name in search.values[0]:
print(name)
Vali, M
Alemane, K
laszik, M
Additionally, the dataframe can be updated with additional columns and or rows and saved back in to a file.
df.to_json('test.json', orient='records')

Get JSON element value using user input in python

I have this JSON data:
{
"id": 1,
"name_company": "Acier Michel",
"inspecteur1": "Hou, L",
"inspecteur2": "Caana, C",
"inspecteur3": "Luc, C",
"type": "Water",
"location": "Laval"
},
{
"id": 2,
"name_company": "Aciers ABC Inc.",
"inspecteur1": "Vali, M",
"inspecteur2": "Alemane, K",
"inspecteur3": "laszik, M",
"type": "NA",
"location": "St-Joseph de Sorel"
}
I want to be able to input "name_company", and get as an output the "inspecteur1" name.
Tried this below but no success..
import json
database = "convertcsv.json"
data = json.loads(open(database).read())
fabricant = input("type company name : ")
for item in database["name_company"]:
if item["name_company"] == fabricant:
print("good")
else:
print("no existant")
You're almost there. A few issues for you to resolve:
Your Json-formatted data is not valid. You seem to be operating on a list, so please ensure that you include square brackets around your data like so:
[
{
"id": 1,
"name_company": "Acier Michel",
"inspecteur1": "Hou, L",
"inspecteur2": "Caana, C",
"inspecteur3": "Luc, C",
"type": "Water",
"location": "Laval"
},
{
"id": 2,
"name_company": "Aciers ABC Inc.",
"inspecteur1": "Vali, M",
"inspecteur2": "Alemane, K",
"inspecteur3": "laszik, M",
"type": "NA",
"location": "St-Joseph de Sorel"
}
]
You're using the filename string variable (database = "convertcsv.json") in your loop declaration, when you instead want to use the Json array from the data you've just read in to your program in order to iterate over each Json object (or, list of dicts in Python-speak). That would be the 'data' variable as you've defined it.
You also want to access the properties of each object (aka. dict) within the loop and not on the iterable itself since that is what the 'item' variable will represent in the loop as you've defined it. So, remove the ["name_company"] bit from the for loop declaration, but leave it in your if condition below.
Provided you've fixed your data, the code below should run:
import json
database = "convertcsv.json"
data = json.loads(open(database).read())
fabricant = input("type company name : ")
for item in data:
if item["name_company"] == fabricant:
print("good")
else:
print("no existant")
To print the inspecteur name, just add the line print(item["inspecteur1"])
import json
database = "convertcsv.json"
data = json.loads(open(database).read())
fabricant = input("type company name : ")
for item in data:
if item["name_company"] == fabricant:
print("good")
print(item["inspecteur1"])
else:
print("no existant")
If you are loading the data using json.loads() then append the json to a list.
data = []
data.append(json.loads(open(database).read())))
Now you can loop over the list and get the key - value pairs like similar to a dictionary
fabricant = input("type company name: ")
for item in data:
if item['name_company'] == str(fabricant):
print(item['inspecteur1'])
else:
print("Not Found")
Below code might help -
Main:
database = "convertcsv.json"
data = json.loads(open(database).read())
user_input_company = 'Aciers ABC Inc.'
records = collection_of_records(data)
for record in records:
if record.name_company == user_input_company :
print(record.inspecteur1)
def collection_of_records(set_of_records):
'''Return a tuple of bundles defined in records
'''
from collections import namedtuple
CompanyRecord = namedtuple('record', 'id name_company inspecteur1 inspecteur2 type location')
records = tuple(
CompanyRecord(company_values['id'], company_values['name_company'], company_values['inspecteur1'], company_values['inspecteur2'],
company_values['type'], company_values['location'])
for company_values in set_of_records['record']
)
return records
convertcsv.json
{
"record": [
{
"id": 1,
"name_company": "Acier Michel",
"inspecteur1": "Hou, L",
"inspecteur2": "Caana, C",
"inspecteur3": "Luc, C",
"type": "Water",
"location": "Laval"
},
{
"id": 2,
"name_company": "Aciers ABC Inc.",
"inspecteur1": "Vali, M",
"inspecteur2": "Alemane, K",
"inspecteur3": "laszik, M",
"type": "NA",
"location": "St-Joseph de Sorel"
}
]
}
References
https://docs.python.org/3/library/collections.html

Converting a table to JSON

I'm building a quiz app in Python 3. The users are shown a bunch of clues and they have to guess the sports star. My current backend is using SQLite, however I want to have a live database using Firebase and so need to change this to JSON format.
The table currently looks like this:
Player Name Difficulty Year Club Apps (Goals)
player_1 easy 2014 - 2017 club x (y)
player_1 easy 2017 - 2019 club_2 x (y)
player_2 medium 2019 - club x (y)
Currently, users select the difficulty. I then run a SQL query that gives me a list of all players with that difficulty. The app then displays to the user the last three columns, which are the clues they need to use to guess the player.
To put in into JSON format, I was thinking it would look something like this
{
"users":
{
"player_1_id":
{
"name": "player_1",
"difficulty": "difficulty",
"year": [year_1, year_2, ..., year_n],
"club": [club_in_year_1, club_in_year_2, ...., club_in_year_n],
"apps": [apps_in_year_1, apps_in_year_2, ..., apps_in_year_n]
},
"player_2_id":
"name": "player_2",
...
}
}
So there can be different number of values within each list of year/club/apps, per player name. I created and ran the following code:
import sqlite3
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
connection = sqlite3.connect("player_database.db")
connection.row_factory = dict_factory
cursor = connection.cursor()
cursor.execute("select * from player_history")
results = cursor.fetchall()
print(results)
connection.close()
but this prints each new row with a separate ID, which isn't quite what I want.
How would I edit this to get my desired outpout?
Here is a basic solution to map your data.
# define a function that takes a data array as argument
def mapData(data = []):
# dict that is used to store aggregated rows
results = {}
# iterate through the data array
for item in data:
# aggreagate values of there is already an entry
# in the results dict for given name
if item["name"] in results.keys():
prev = results[item["name"]]
prev["year"].append(item["year"])
prev["club"].append(item["club"])
prev["apps"].append(item["apps"])
# continue will skip to next iteration
continue
# create a new entry if there is no existing key
# for given name in the results dict
results[item["name"]] = {
**item,
"year": [item["year"]],
"club": [item["club"]],
"apps": [item["apps"]],
}
# return the result
return results
# some fake data that looks like the data from sql
sqlData= [
{
"name": "player_1",
"difficulty": "difficulty",
"year": "2009",
"club": "2003",
"apps": "2005",
},
{
"name": "player_1",
"difficulty": "difficulty",
"year": "1999",
"club": "1998",
"apps": "2008",
},
{
"name": "player_2",
"difficulty": "difficulty",
"year": "1999",
"club": "1998",
"apps": "2008",
}
]
# pass the data into the function and assign the result
# to a new variable called aggregatedData
aggregatedData = mapData(sqlData)
# print the result
print(aggregatedData)
# result:
# {
# "player_1":{
# "name":"player_1",
# "difficulty":"difficulty",
# "year":[
# "2009",
# "1999"
# ],
# "club":[
# "2003",
# "1998"
# ],
# "apps":[
# "2005",
# "2008"
# ]
# },
# "player_2":{
# "name":"player_2",
# "difficulty":"difficulty",
# "year":[
# "1999"
# ],
# "club":[
# "1998"
# ],
# "apps":[
# "2008"
# ]
# }
# }

Translate SQL to Python, if possible

Is there a tool to convert a sql statement into python, if it's possible. For example:
(CASE WHEN var = 2 then 'Yes' else 'No' END) custom_var
==>
customVar = 'Yes' if var == 2 else 'No'
I am trying to provide a API for ETL-like transformations from a json input. Here's an example of an input:
{
"ID": 4,
"Name": "David",
"Transformation: "NewField = CONCAT (ID, Name)"
}
And we would translate this into:
{
"ID": 4,
"Name": "David",
"NewField: "4David"
}
Or, is there a better transformation language that could be used here over SQL?
Is SET NewField = CONCAT (ID, Name) actually valid sql? (if Newfield is a variable do you need to declare it and prefix with "#"?). If you want to just execute arbitrary SQL, you could hack something together with sqlite:
import sqlite3
import json
query = """
{
"ID": "4",
"Name": "David",
"Transformation": "SELECT ID || Name AS NewField FROM inputdata"
}"""
query_dict = json.loads(query)
db = sqlite3.Connection('mydb')
db.execute('create table inputdata ({} VARCHAR(100));'.format(' VARCHAR(100), '.join(query_dict.keys())))
db.execute('insert into inputdata ({}) values ("{}")'.format(','.join(query_dict.keys()),'","'.join(query_dict.values())))
r = db.execute(query_dict['Transformation'])
response = {}
response[r.description[0][0]] = r.fetchone()[0]
print(response)
#{'NewField': '4David'}
db.execute('drop table inputdata;')
db.close()

Categories