Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with reading json file
#1
Hi,
Can anyone help me to review this short code below that read json data from url?
I am not sure why it give error.
Thank you,
HHC

data = pd.read_json('http://hl7.org/fhir/2018Sep/invoice-example.json')
Reply
#2
what was the error, please show unmodified and complete error traceback (bound by bbcode tags)
Reply
#3
The whole traceback is:
Error:
Traceback (most recent call last): File "<input>", line 1, in <module> File "D:\Python\Homework\venv\lib\site-packages\pandas\util\_decorators.py", line 199, in wrapper return func(*args, **kwargs) File "D:\Python\Homework\venv\lib\site-packages\pandas\util\_decorators.py", line 299, in wrapper return func(*args, **kwargs) File "D:\Python\Homework\venv\lib\site-packages\pandas\io\json\_json.py", line 563, in read_json return json_reader.read() File "D:\Python\Homework\venv\lib\site-packages\pandas\io\json\_json.py", line 694, in read obj = self._get_object_parser(self.data) File "D:\Python\Homework\venv\lib\site-packages\pandas\io\json\_json.py", line 716, in _get_object_parser obj = FrameParser(json, **kwargs).parse() File "D:\Python\Homework\venv\lib\site-packages\pandas\io\json\_json.py", line 831, in parse self._parse_no_numpy() File "D:\Python\Homework\venv\lib\site-packages\pandas\io\json\_json.py", line 1078, in _parse_no_numpy self.obj = DataFrame( File "D:\Python\Homework\venv\lib\site-packages\pandas\core\frame.py", line 529, in __init__ mgr = init_dict(data, index, columns, dtype=dtype) File "D:\Python\Homework\venv\lib\site-packages\pandas\core\internals\construction.py", line 287, in init_dict return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype) File "D:\Python\Homework\venv\lib\site-packages\pandas\core\internals\construction.py", line 80, in arrays_to_mgr index = extract_index(arrays) File "D:\Python\Homework\venv\lib\site-packages\pandas\core\internals\construction.py", line 404, in extract_index raise ValueError( ValueError: Mixing dicts with non-Series may lead to ambiguous ordering.
buran write Jul-07-2021, 07:06 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#4
Exactly what datafarme would you expect to produce from this JSON? It doesn't look suitable for processing as dataframe

Why not simply read the JSON and then process it?

import requests

response = requests.get('http://hl7.org/fhir/2018Sep/invoice-example.json')
data = response.json()

print(data)
print(f"Total Gross: {data['totalGross']['currency']} {data['totalGross']['value']}")
Output:
{'resourceType': 'Invoice', 'id': 'example', 'text': {'status': 'generated', 'div': '<div xmlns="http://www.w3.org/1999/xhtml">Example of Invoice</div>'}, 'identifier': [{'system': 'http://myHospital.org/Invoices', 'value': '654321'}], 'status': 'issued', 'subject': {'reference': 'Patient/example'}, 'date': '2017-01-25T08:00:00+01:00', 'participant': [{'role': {'coding': [{'system': 'http://snomed.info/sct', 'code': '17561000', 'display': 'Cardiologist'}]}, 'actor': {'reference': 'Practitioner/example'}}], 'issuer': {'identifier': {'system': 'http://myhospital/NamingSystem/departments', 'value': 'CARD_INTERMEDIATE_CARE'}}, 'account': {'reference': 'Account/example'}, 'totalNet': {'value': 40, 'currency': 'EUR'}, 'totalGross': {'value': 48, 'currency': 'EUR'}} Total Gross: EUR 48
hhchenfx and Larz60+ like this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
To read it do.
data = pd.read_json('http://hl7.org/fhir/2018Sep/invoice-example.json', orient='index')
Quick test NoteBook
I just mess around a transpose,the way to go is look at Pandas json_normalize(),as in post under.
How to parse JSON data with Python Pandas?
hhchenfx likes this post
Reply
#6
Thank you all for helping.
I understand it now.
HHC
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question In need of insight regarding Python file reading mechanisms. EnfantNicolas 7 199 Sep-18-2021, 10:39 AM
Last Post: ndc85430
  [Solved] Reading every nth line into a column from txt file Laplace12 7 770 Jun-29-2021, 09:17 AM
Last Post: Laplace12
  help with project of reading and searching big log file korenron 6 388 Jun-24-2021, 01:57 PM
Last Post: korenron
  Helps with reading csv file - 3 methods hhchenfx 4 664 May-13-2021, 04:15 AM
Last Post: buran
  Subprocess.Popen() not working when reading file path from csv file herwin 13 1,696 May-07-2021, 03:26 PM
Last Post: herwin
  Request for Python code - Features extraction from JSON file (cuckoo) thinker 1 557 Apr-07-2021, 04:40 PM
Last Post: Larz60+
  find the header location in a .bin file without reading the whole file at a time SANJIB 0 569 Mar-05-2021, 04:08 PM
Last Post: SANJIB
  Reading a csv file Led_Zeppelin 2 786 Feb-26-2021, 05:48 AM
Last Post: buran
  reading a csv file Led_Zeppelin 3 986 Feb-19-2021, 02:16 PM
Last Post: Led_Zeppelin
  Sort data from JSON file Dummy_in_programming 2 671 Jan-04-2021, 06:17 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