Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to read json file
#31
ooof... this is a rough read! you're taking a java class (probably intro oop class)... what makes you think submitting a solution in python will suffice?
Reply
#32
(Feb-24-2020, 07:45 PM)snippsat Wrote: Use Code Tag,and there is no indentation in code you have posted.
There is mix of two diffent quotation marks in the json sample,that will never work.
"category
jk91 Wrote:but how does it match with my problem,i mean here i have very very complex data model like below:-
You struggle even with the most basic stuff as usage of OS,files...ect,have you looked into and learned some basic Python?
If this is task you have gotten,ask people who have given it you to help get started.

code (data model used in the problem) with brackets:-
The data model for the database tables is defined as follows.
Data model
[code]
items: id(pkey), name
categories: id(pkey), name
category_items: id(pkey), category_id (fkey categories), item_id(fkey items)
menu.json
{
"category”:[{
“name”: “Appetizer”,
},
{
“name”: “Entree”
}
]
],
"item”:[
{
"name":"French Fries",
"category":"Appetizer"
},
{
"name":"Onion Rings",
"category":"Appetizer"
},
{
"name":"Sandwich",
"category":"Entree"
},
{
"name":"Tacos",
"category":"Entree"
},
{
"name":"Ice Cream Sundae",
"category":"Dessert"
}
],
"restaurant":"Joe's Grill"
}
}
Existing classes:
class category:
def __init__(self, name):
self.name = name
class item:
def __init__(self, name, category_name):
self.name = name
self.category_name = category_name
Helper Functions that Already exist
// json.get_categories(file_name) -> returns array of active category objects (category.name)
// json.get_items(file_name) --> returns array of item object (item.name, item.category_name)
// db.write(table_name, <a data dict or an array of data dicts>) //db.write('categories', {'name':
'c1'})

[code/]
Note:The keys of the dictionary are the table column names, the values are the row values in the database table.

(Feb-25-2020, 04:11 PM)jk91 Wrote:
(Feb-24-2020, 07:45 PM)snippsat Wrote: Use Code Tag,and there is no indentation in code you have posted.
There is mix of two diffent quotation marks in the json sample,that will never work.
"category
You struggle even with the most basic stuff as usage of OS,files...ect,have you looked into and learned some basic Python?
If this is task you have gotten,ask people who have given it you to help get started.

code (data model used in the problem) with brackets:-
The data model for the database tables is defined as follows.
Data model
[code]
items: id(pkey), name
categories: id(pkey), name
category_items: id(pkey), category_id (fkey categories), item_id(fkey items)
menu.json
{
"category”:[{
“name”: “Appetizer”,
},
{
“name”: “Entree”
}
]
],
"item”:[
{
"name":"French Fries",
"category":"Appetizer"
},
{
"name":"Onion Rings",
"category":"Appetizer"
},
{
"name":"Sandwich",
"category":"Entree"
},
{
"name":"Tacos",
"category":"Entree"
},
{
"name":"Ice Cream Sundae",
"category":"Dessert"
}
],
"restaurant":"Joe's Grill"
}
}
Existing classes:
class category:
def __init__(self, name):
self.name = name
class item:
def __init__(self, name, category_name):
self.name = name
self.category_name = category_name
Helper Functions that Already exist
// json.get_categories(file_name) -> returns array of active category objects (category.name)
// json.get_items(file_name) --> returns array of item object (item.name, item.category_name)
// db.write(table_name, <a data dict or an array of data dicts>) //db.write('categories', {'name':
'c1'})

[code/]
Note:The keys of the dictionary are the table column names, the values are the row values in the database table.

there is a mix of two different quotation marks in the json sample,that will never work.
"category what should it be in a correct way then it might be deliberately so that it hould be a very complex case isn't it?
Reply
#33
Here ya go

items: id(pkey), name
categories: id(pkey), name
category_items: id(pkey), category_id (fkey categories), item_id(fkey items)
menu.json
{
"category”:[{
“name”: “Appetizer”,
},
{
“name”: “Entree”
}
]
],
"item”:[
{
"name":"French Fries",
"category":"Appetizer"
},
{
"name":"Onion Rings",
"category":"Appetizer"
},
{
"name":"Sandwich",
"category":"Entree"
},
{
"name":"Tacos",
"category":"Entree"
},
{
"name":"Ice Cream Sundae",
"category":"Dessert"
}
],
"restaurant":"Joe's Grill"
}
}
Existing classes:
class category:
def __init__(self, name):
self.name = name
class item:
def __init__(self, name, category_name):
self.name = name
self.category_name = category_name
Helper Functions that Already exist
// json.get_categories(file_name) -> returns array of active category objects (category.name)
// json.get_items(file_name) --> returns array of item object (item.name, item.category_name)
// db.write(table_name, <a data dict or an array of data dicts>) //db.write('categories', {'name':
'c1'})
Reply
#34
(Feb-25-2020, 04:11 PM)jk91 Wrote: "category” what should it be in a correct way then it might be deliberately so that it hould be a very complex case isn't it?
Sure it can have be placed there deliberately,so you shall clean it up.
You have given little info about the task,and all of this Thread is mostly been trying for you to open a working json file.

Alone it will be SyntaxError
>>> s = "category”
  File "<interactive input>", line 1
    s = "category”
                 ^
SyntaxError: EOL while scanning string literal
Reading it with json.load it will be JSONDecodeError.
So have to read file in a normal way so it's string output an clean it up before it's a working json file.
The cleaning up would be like this.
>>> s = '"category”'
>>> s
'"category”'
>>> s1 = s.replace('”', '"')
>>> s1
'"category"'
So if i mess up data.json with ,i would clean it up like this.
>>> with open('data.json', encoding='utf-8') as f:
...     file_content = f.read()

>>> fix = file_content.replace('”', '"')

>>> with open('fixed.json', 'w', encoding='utf-8') as f_out:
...     f_out.write(fix)
Reply
#35
(Feb-25-2020, 06:24 PM)t4keheart Wrote: Here ya go

items: id(pkey), name
categories: id(pkey), name
category_items: id(pkey), category_id (fkey categories), item_id(fkey items)
menu.json
{
"category”:[{
“name”: “Appetizer”,
},
{
“name”: “Entree”
}
]
],
"item”:[
{
"name":"French Fries",
"category":"Appetizer"
},
{
"name":"Onion Rings",
"category":"Appetizer"
},
{
"name":"Sandwich",
"category":"Entree"
},
{
"name":"Tacos",
"category":"Entree"
},
{
"name":"Ice Cream Sundae",
"category":"Dessert"
}
],
"restaurant":"Joe's Grill"
}
}
Existing classes:
class category:
def __init__(self, name):
self.name = name
class item:
def __init__(self, name, category_name):
self.name = name
self.category_name = category_name
Helper Functions that Already exist
// json.get_categories(file_name) -> returns array of active category objects (category.name)
// json.get_items(file_name) --> returns array of item object (item.name, item.category_name)
// db.write(table_name, <a data dict or an array of data dicts>) //db.write('categories', {'name':
'c1'})
actually i think you misunderstood.I meant to say that what should i ask or what exact how should i ask to correct this problem statement with respect to categories here , i mean how should i explain what is exactly wrong here in categories and how should it be to correct it to solve it?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Recommended way to read/create PDF file? Winfried 3 2,783 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  parse json field from csv file lebossejames 4 668 Nov-14-2023, 11:34 PM
Last Post: snippsat
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,308 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 1,047 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,043 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,160 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 6,006 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Python Script to convert Json to CSV file chvsnarayana 8 2,343 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 1,960 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Read csv file with inconsistent delimiter gracenz 2 1,140 Mar-27-2023, 08:59 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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