Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Converting dictionary string to dictionary failed

I tried to convert below string to dictionary by json.loads() and ast.literal_eval method but both failed, please kindly help:

text = '{"data":{"FormId":"BD_Rate","TopRowCount":0,"Limit":0,"StartRow":0,"FilterString":'',"OrderString":'',"FieldKeys":"FRateID,FDocumentStatus,FForbidStatus,FName,FDescription,FCreateOrgId,FUseOrgId,FCreatorId,FModifierId,FCreateDate,FModifyDate,FBegDate,FCyToID,FEndDate,FRATETYPEID,FCyForID,FAuditDate,FAuditorID,FForbidderID,FForbidDate,FIsSysPreset,FExchangeRate,FReverseExRate"}}'

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Your string is quoted using single quotes (') and you also have "OrderString":'' as part of your string. Python will remove those '' because it will assume string concatenation. For example:

s = 'foo''bar'

is equivalent to

s = 'foo' + 'bar'

hence the same thing as

s = 'foobar'

Change your first line to

text = '{"data":{"FormId":"BD_Rate","TopRowCount":0,"Limit":0,"StartRow":0,"FilterString":"","OrderString":"","FieldKeys":"FRateID,FDocumentStatus,FForbidStatus,FName,FDescription,FCreateOrgId,FUseOrgId,FCreatorId,FModifierId,FCreateDate,FModifyDate,FBegDate,FCyToID,FEndDate,FRATETYPEID,FCyForID,FAuditDate,FAuditorID,FForbidderID,FForbidDate,FIsSysPreset,FExchangeRate,FReverseExRate"}}'

and both json and ast.literal_eval will work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading