Python Forum
how to read json file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how to read json file (/thread-24535.html)

Pages: 1 2 3 4


how to read json file - jk91 - Feb-18-2020

Hi,

how to write a very tough program code for below problem:

Utility to read JSON file from file server

2. Utility should run at scheduled time let’s say 6AM

3. Error message if JSON file is not properly formatted

4. Error message if Category is missing and ITEM need that Category to be saved.

5. Entity mapping as per the given relationship in data model

5. Documentation for the API, preferably using any tool

6. Junit Test cases using Mockito

7. Use either MySQL or Oracle for API development

8. add one point as JSON validation for not null and value range.

Thanks


RE: how to read json file - Larz60+ - Feb-18-2020

What have you tried so far?


RE: how to read json file - ndc85430 - Feb-18-2020

Also, are you supposed to be working in Java (or another JVM language) as point 6 would imply? If so, why is this on a Python forum?


RE: how to read json file - jk91 - Feb-19-2020

so can't same goal not be achieved using python?

any updates by python experts please?


RE: how to read json file - ndc85430 - Feb-19-2020

Of course it can, it just seems that your instructions are implying the JVM. Naturally, I don't know the details of your course; I'm just going by what you've presented to us.

As for updates, you've been asked above what you've tried. You've still not shown us anything. We're not here to do your work for you, but we're happy to help when you're stuck. If you're struggling so much that you can't even begin the task, then perhaps you should speak to whoever gave you this assignment.


RE: how to read json file - jk91 - Feb-19-2020

to nswer first question of this problem tried below:-

Create a class named "JSONRead" in eclipse. In this we will using "JSONParser" to convert the JSON string in the file to JSONOBject.
 In order to use JSON Parser makes sure that your string is in JSON format.


package logicProgramming;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JsonRead {

	public static void main(String[] args) {
		
		JSONParser parser = new JSONParser();
		//JsonParser to convert JSON string into Json Object

		try {
			Object obj = parser.parse(new FileReader("g:\\newfile.json"));
			//parsing the JSON string inside the file that we created earlier.

			JSONObject jsonObject = (JSONObject) obj;
			System.out.println(jsonObject);
			//Json string has been converted into JSONObject

			String name = (String) jsonObject.get("name");
			System.out.println(name);

			String department = (String) jsonObject.get("department");
			System.out.println(department);

			String branch = (String) jsonObject.get("branch");
			System.out.println(branch);

			long year = (long) jsonObject.get("year");
			System.out.println(year);
			//Displaying values from JSON OBject by using Keys

			JSONArray remarks = (JSONArray) jsonObject.get("remarks");
			//converting the JSONObject into JSONArray as remark was an array.
			Iterator<String> iterator = remarks.iterator();
			//Iterator is used to access the each element in the list 
			//loop will continue as long as there are elements in the array.
			while (iterator.hasNext()) {
				System.out.println(iterator.next());
				//accessing each elemnt by using next function.
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

}



RE: how to read json file - jk91 - Feb-20-2020

actually we need to cover these points :-
1. Utility to read JSON file from file server

2. Utility should run at scheduled time let’s say 6AM

3. Error message if JSON file is not properly formatted

4. Error message if Category is missing and ITEM need that Category to be saved.

5. Entity mapping as per the given relationship in data model

5. Documentation for the API, preferably using any tool

6. Junit Test cases using Mockito

7. Use either MySQL or Oracle for API development

8. add one point as JSON validation for not null and value range
******************
Validation Points:



Step 1: You have to read the file, If any Error, please correct.

Step 2: IF any category/item Missing, Null Message came or Any notification.

Step 3: Add the category and save the message,

Step 4: if Order not received it 6 AM, then Not file Uploaded Message must show OR repeat the previous order.

Step 5: Must add clock in Programmer.

Introduction
Someone wants to manage their website. One of
the most important pages on their website is the “Menu” page.The menu gets updated every
night at the restaurant and Joe’s grill wants the updated menu published to their website every
morning. customer will upload their menu in a JSON formatted file (Let’s call it menu.json) to our
ftp server every night.
Requirement is to develop a program to take this menu.json file and store the data
in a relational database. The data model for the database tables is defined as follows.
Data model
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'})
Note:The keys of the dictionary are the table column names, the values are the row values in
the database table

so how to write program for the same now?

thanks


RE: how to read json file - jk91 - Feb-20-2020

Any updates by experts please?

Thanks


RE: how to read json file - buran - Feb-20-2020

Bumping from time to time is OK, but don't do it every couple of hours or so.

You should ask yourself Why people don't answer?. Is it because it's unclear, too broad and vague? Or because you don't show effort on your own? Or all of it?


RE: how to read json file - snippsat - Feb-20-2020

Look like you have been given this a task as homework.
With some Java code as a start and that you should write it Python.
So is this homework?

Just to give some hint on a start.
jk91 Wrote:Step 1: You have to read the file, If any Error, please correct.
import json

with open("data.json", "r") as read_file:
    data = json.load(read_file)
This read the json file,look into try:except for catching error.

What the json file return is Python dictionary,here is it normal to have a mix of dict and list as your data show.
Can use direct access or a get method as the Java code has.
>>> data
{'age': 35,
 'children': [{'age': 6, 'firstName': 'Alice'}, {'age': 8, 'firstName': 'Bob'}],
 'firstName': 'Jane',
 'hobbies': ['running', 'sky diving', 'singing'],
 'lastName': 'Doe'}

>>> # Direct access
>>> data['children'][0]['firstName']
'Alice'
>>> 
>>> # Using get here can also do error check
>>> data['children'][0].get('firstName', 'Not in Record')
'Alice'
>>> data['children'][0].get('firstName9', 'Not in Record')
'Not in Record'