-1

So i have a Json file like this:

{"matches_details": [{"MatchID": "Liverpool-Manchester United", "Date": "17/12/2020", "Time": "16:00", "Score": "5-0", "Yellow cards": "2", "Red cards": "1", "State": "FT"}, {"MatchID": "Crystal Palace-Chelsea", "Date": "20/1/2021", "Time": "11:00", "Score": "10-0", "Yellow cards": "10", "Red cards": "20", "State": "HT"}]}

The way to print out all the array is:

f = open('datngu.json',)
    data = json.load(f)
    for i in data['matches_details']:
        print(i)

    f.close()

And the way to print a single one:

print(data['matches_details']['MatchID'])

So how can i change it into a 2d array with row,col format, because i need to insert these data into a pyqt5 qtTableWidgetItem UI

1

2 Answers 2

1

You can try the following if you would like to have it as a pd.DataFrame():

import pandas as pd

values = {"matches_details": [{"MatchID": "Liverpool-Manchester United", "Date": "17/12/2020", "Time": "16:00", "Score": "5-0", "Yellow cards": "2", "Red cards": "1", "State": "FT"}, {"MatchID": "Crystal Palace-Chelsea", "Date": "20/1/2021", "Time": "11:00", "Score": "10-0", "Yellow cards": "10", "Red cards": "20", "State": "HT"}]}

df_table = pd.DataFrame(values['matches_details'])

If you prefer it as a numpy 2D-array, just do the following:

arr_2d = df_table.values
Sign up to request clarification or add additional context in comments.

2 Comments

so what is the 1st follwing about
The first part helps you to put the json in a DataFrame format (a table).
0

You can try to use numpy as follows:

m = np.zeros((len(match_details_json), 7))
for i, elem in enumerate(match_details_json):
    m[i,0] = elem['MatchID']
    m[i,1] = elem['Date']
    m[i,2] = elem['Time']
    m[i,3] = elem['Score']
    m[i,4] = elem['Yellow cards']
    m[i,5] = elem['Red cards']
    m[i,5] = elem['State']

2 Comments

what is 'match_details_json'?
This will be the json string that you want to convert to array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.