question

Upvotes
Accepted
1 0 1 1

Issue while flattening the JSON file to CSV in RDP ESG Bulk

Hello there,

I did ESG Bulk extraction. However, when I am trying to flatten the JSON to CSV, few columns still has the json tags. It doesn't seem to flatten the JSON file completely. Appreciate you support to investigate and help on this.

For instance, I tried converting the RFT-ESG-Scores-Full-Init-2021-04-25.jsonl.gz file from JSON to CSV using the following code,

#convert specific json to csv
filedestinationpath = 'C:\\$files\\$ESG\\RDP_BULK\\Results\\'
filename = filedestinationpath + 'RFT-ESG-Scores-Full-Init-2021-04-25' + '.jsonl.gz'
f=gzip.open(filename,'rb')
file_content=f.read()
lines = file_content.splitlines()
df_inter = pd.DataFrame(lines)
df_inter.columns = ['json_element']
df_resolve = df_inter['json_element'].apply(json.loads)
df_final = pd.json_normalize(df_resolve)
resultspth = filedestinationpath + 'RFT-ESG-Scores-Full-Init-2021-04-25' + '.csv'
df_final.to_csv(resultspth, index = False)

It seem to convert, but not all the column. For example, OrganizationName doesn't seem to flatten out completely, it still carries the json tags.


Similarly, when I tried "RFT-ESG-Symbology-SEDOL-Init-2021-04-29.jsonl"; few columns as shown in the screengrab below seems to be the issue.

Appreciate if you can review and support on this.

rdp-apirefinitiv-data-platformesgbulk-download
esg-error1.jpg (207.8 KiB)
esg-error3.jpg (226.6 KiB)
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Upvotes
Accepted
23k 22 9 14

Hello @Bala Ilango,

Perhaps you may wish to take the same approach further, and double-normalize the fields that contain nested objects on RFT-ESG-Scores:

For example:

import gzip
import pandas as pd
import json
#convert specific json to csv
filedestinationpath = '.\\'
filename = filedestinationpath + 'RFT-ESG-Scores-Current-init-2021-05-02' + '.jsonl.gz'
f=gzip.open(filename,'rb')
file_content=f.read()
lines = file_content.splitlines()
df_inter = pd.DataFrame(lines)
df_inter.columns = ['json_element']
df_resolve = df_inter['json_element'].apply(json.loads)
df_resolve
df_final = pd.json_normalize(df_resolve)
df_final['ESGOrganization.Names.Name.OrganizationName'] = pd.json_normalize(df_final['ESGOrganization.Names.Name.OrganizationName'].str[0]
resultspth = filedestinationpath + 'RFT-ESG-Scores-Current-init-2021-05-02' + '.csv'
df_final.to_csv(resultspth, index = False) 
df_final 

Resulting in



normalizenested.gif (57.8 KiB)
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

@zoya.farberov When I am running the following line in the code.

df_final['ESGOrganization.Names.Name.OrganizationName'] = pd.json.normalize(df_final['ESGOrganization.Names.Name.OrganizationName'].str[0])

I am getting the following error:

AttributeError: module 'pandas' has no attribute 'json'

@Bala Ilango

try pd.json_normalize

pd.json.normalize appears to be a typo

Upvotes
1.4k 6 2 2

Hi @Bala Ilango,

May I ask how you got the 'RFT-ESG-Scores-Full-Init-2021-04-25.jsonl.gz' data file in question? was it through Python? If so, would you mind sharing your code (removing identifiable text)?

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Upvotes
1 0 1 1

@jonathan.legrand Yes. I am using Python. Shared the codes via email. Appreciate your support with this. Thanks.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Upvotes
23k 22 9 14

Hello @Bala Ilango,

The columns that do not convert, and retain square brackets and curly braces are nested objects.

ESGOrganization.Names.Name.OrganizationName is in this case a nested object, implementing array and containing a map, with, potentially, multiple names, for example:

{"ObjectId":"4295864969;111","StatementDetails":{"OrganizationId":"4295864969","FinancialPeriodEndDate":"2020-12-31T00:00:00.000Z","FinancialPeriodFiscalYear":"2020","FinancialPeriodIsIncomplete":"true"},"ESGOrganization":{"Names":{"Name":{"OrganizationName":[{"OrganizationNormalizedName":"China High Speed Transmission Equipment Group Co Ltd"}]}}},"ESGScores":{"ESGCombinedScore":{"Value":"0.5429413762056495","ValueCalculationDate":"2021-05-01T18:05:32.161Z","ValueScoreGrade":"B-"},"ESGScore":{"Value":"0.5429413762056495","ValueCalculationDate":"2021-05-01T18:05:32.161Z","ValueScoreGrade":"B-"},"EnvironmentPillarScore":{"Value":"0.6736242884250474","ValueCalculationDate":"2021-04-24T17:12:23.608Z","ValueScoreGrade":"B+ "},"ESGResourceUseScore":{"Value":"0.7903225806451613","ValueCalculationDate":"2021-04-03T21:34:07.385Z","ValueScoreGrade":"A-"},"ESGEmissionsScore":{"Value":"0.7258064516129032","ValueCalculationDate":"2021-04-24T17:12:23.608Z","ValueScoreGrade":"B+"},"ESGInnovationScore":{"Value":"0.5","ValueCalculationDate":"2021-04-03T21:34:07.385Z","ValueScoreGrade":"C+"},"SocialPillarScore":{"Value":"0.4414690382081688","ValueCalculationDate":"2021-04-24T17:12:23.608Z","ValueScoreGrade":"C+ "},"ESGWorkforceScore":{"Value":"0.6375","ValueCalculationDate":"2021-04-10T17:37:21.596Z","ValueScoreGrade":"B"},"ESGHumanRightsScore":{"Value":"0.15217391304347827","ValueCalculationDate":"2021-04-03T21:34:07.385Z","ValueScoreGrade":"D"},"ESGCommunityScore":{"Value":"0.4375","ValueCalculationDate":"2021-04-24T17:12:23.608Z","ValueScoreGrade":"C+"},"ESGProductResponsibilityScore":{"Value":"0.3484848484848485","ValueCalculationDate":"2021-04-10T17:37:21.596Z","ValueScoreGrade":"C"},"GovernancePillarScore":{"Value":"0.4760119460883173","ValueCalculationDate":"2021-05-01T18:05:32.161Z","ValueScoreGrade":"C+ "},"ESGManagementScore":{"Value":"0.5246305418719212","ValueCalculationDate":"2021-05-01T18:05:32.161Z","ValueScoreGrade":"B-"},"ESGShareholdersScore":{"Value":"0.46798029556650245","ValueCalculationDate":"2021-04-10T17:37:21.596Z","ValueScoreGrade":"C+"},"ESGCsrStrategyScore":{"Value":"0.24496644295302014","ValueCalculationDate":"2021-04-10T17:37:21.596Z","ValueScoreGrade":"D+"},"ESGCControversiesScore":{"Value":"1.0","ValueCalculationDate":"2021-04-03T21:34:07.385Z","ValueScoreGrade":"A+"}},"DiversityAndInclusionScores":{"ControversiesScore":{"Value":null,"ValueCalculationDate":null},"DiversityScore":{"Value":null,"ValueCalculationDate":null},"InclusionScore":{"Value":null,"ValueCalculationDate":null},"PeopleDevelopmentScore":{"Value":null,"ValueCalculationDate":null},"Score":{"Value":null,"ValueCalculationDate":null}}}

Therefore, it will not fully flatten into CSV, without loosing meaning. If you are ok with only the first name represented, you can do something like

df = pd.json_normalize(df_resolve)
df['ESGOrganization.Names.Name.OrganizationName'] = df['ESGOrganization.Names.Name.OrganizationName'].str[0]

And similarly, you can select the key and the value and insert them into dataframe as separate columns, removing the hierarchical column, but I think you would like to preserve the structure in this case, as it enables you to retain the meaning.



icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

@zoya.farberov Can you please help me with my other part of the question on best practice to flatten the ESG bulk Symbology files.

Upvote
23k 22 9 14

Hello @Bala Ilango,

To save ESG Bulk symbology files in CSV format, you may also find example ESGBulkToCSV on GutHub useful.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Click below to post an Idea Post Idea