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
  encrypt data in json file help jacksfrustration 1 70 Yesterday, 05:16 PM
Last Post: deanhystad
Sad problems with reading csv file. MassiJames 3 559 Nov-16-2023, 03:41 PM
Last Post: snippsat
  parse json field from csv file lebossejames 4 669 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Reading a file name fron a folder on my desktop Fiona 4 851 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  TypeRoor reading json GreenLynx 3 802 May-16-2023, 01:47 PM
Last Post: buran
  Python Script to convert Json to CSV file chvsnarayana 8 2,347 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 1,962 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,048 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,178 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  Reading a file JonWayn 3 1,059 Dec-30-2022, 10:18 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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