SoFunction
Updated on 2024-12-17

How to deal with key in json without double quotes question

Handling json without double quotes key problem

When parsing json data on a web page, I found that the python standard library json module could not load the data.

As the following data

import json
s='{symbol:"sh600069",code:"600069",name:"Silver Dove Investments.",trade:"3.160",pricechange:"-0.030",changepercent:"-0.940",buy:"3.160",sell:"3.170",settlement:"3.190",open:"3.190",high:"3.210",low:"3.140",volume:3905810,amount:12388386,ticktime:"15:00:00",per:79,pb:2.416,mktcap:513131.494704,nmc:513131.494704,turnoverratio:0.24053}'
js=(s)

The result is an error.

The expected attribute name should be in double quotes, python can't recognize this kind of data without double quotes in json:

How do we fix it?

The first method is to use a third-party library demjson. this installation directly after the decode data source on it.

The other is to write your own custom function to deal with it, and today I found a custom function that deals with this kind of problem.

def jsonfy(s:str)->object:
    # This function normalizes the key of a json without double quotes.
    obj = eval(s, type('js', (dict,), dict(__getitem__=lambda s, n: n))())
    return obj

The word json string with a function to standardize the name of the attributes found inside the quotes, and has been converted to a dictionary object. Can be directly manipulated to obtain the number of .

Of course a bit of regularization is no problem. The main thing is to exclude the time string (time just happens to have: inside it, which is also the key-value separator of json)::.

json data key value with quotes and without quotes value

After getting the data from the backend I encountered a bug: (can't get the value of the key with quotes)

Some json data has keys without quotes

{pk: "0000c972-ff4c-11e8-aa32-08002782624e", name: "Wulitian, Yunhong Street, Leling City", longitude: 0, latitude: 0, ph: 5.8, …}

Some return values have keys that are quoted by the

{"type":"Feature","properties":{"name": "Wulitian, Yunhong Street, Leling City"},"geometry":{"type": "Polygon","coordinates":[[[117.25291089900009,37.74238802900004],[117.25291973100002,37.742300420000049],[117.25345277500003,37.742330535000065],[117.25353190700002,37.742335003000051],[117.25458942300008,37.742394742000045],[117.25606973600009,37.742478319000043],[117.25619680700004,37.74248549400005],[117.25628420100008,37.742490429000043],[117.25640651900005,37.74136234100007],[117.25641165700006,37.741341648000059],[117.25641504000009,37.74131912200005],[117.25653114400006,37.740545954000027],[117.25653071400006,37.740421641000069],[117.25653068100007,37.740412453000033],[117.25653560100011,37.740307598000072],[117.25655447300005,37.740229446000058],[117.25655915900006,37.740201246000026],[117.25659345800011,37.739994860000024],[117.25684111700002,37.740067059000069],[117.2581451420001,37.740602798000054],[117.25818614700006,37.740444250000053],[117.25853526600008,37.739094402000035],[117.25846830700004,37.739081966000072],[117.25785620500005,37.738968281000041],[117.25781732600001,37.738961486000051],[117.25713602600001,37.738842377000026],[117.25686070600011,37.738794245000065],[117.25671882300003,37.738769440000056],[117.2568146970001,37.738190186000054],[117.2569643170001,37.736881095000058],[117.25698481000006,37.736701808000078],[117.25699099500002,37.73664769800007],[117.25699661300007,37.73660231100007],[117.25705518100006,37.736129216000052],[117.25707020300001,37.736007876000031],[117.25708566700007,37.735882976000028],[117.25709686500011,37.735796107000056],[117.25699141300004,37.735788132000039],[117.25638904900006,37.735742576000064],[117.25641200300004,37.735421416000065],[117.25641212300002,37.735419738000076],[117.25636351900005,37.735415323000041],[117.25612242400007,37.735393424000051],[117.25606116100005,37.735677287000044],[117.25561191400004,37.735623850000025],[117.25516524800003,37.735570715000051],[117.25516742700006,37.735611687000073],[117.25516171400011,37.735611452000057],[117.25502474000007,37.735605814000053],[117.25502242800007,37.735605719000034],[117.25428839000006,37.735550486000079],[117.25428113200007,37.735549936000041],[117.25331845700009,37.735476772000027],[117.25323248000007,37.735470238000062],[117.25318180400006,37.735911238000028],[117.25316272300006,37.73599501800004],[117.25316128200006,37.736001351000027],[117.25266654100005,37.735996176000072],[117.25265935100003,37.736096306000036],[117.25262183000007,37.736092756000062],[117.25231804500004,37.736064017000047],[117.25231533300007,37.736063760000036],[117.25213678200009,37.736031242000024],[117.25197550200005,37.736021422000078],[117.25193599600004,37.736019019000025],[117.25133025500008,37.735992714000076],[117.25084230200002,37.735912430000042],[117.25084119200005,37.735918906000052],[117.25081736800007,37.736057886000026],[117.25082260200008,37.736102301000074],[117.25087354300001,37.736126425000066],[117.25093976400001,37.736158596000053],[117.25087864600005,37.73631268400004],[117.25085193300004,37.736415238000063],[117.25081422300002,37.736631421000027],[117.25070216000006,37.73661532300008],[117.25056686900007,37.736595890000046],[117.25056490100008,37.736595607000027],[117.25046782600009,37.736997209000037],[117.25043061400004,37.737151148000066],[117.25043060300004,37.737151193000045],[117.25048086900006,37.73716498400006],[117.2504110110001,37.737501510000072],[117.25036900700002,37.737703829000054],[117.25035327700004,37.737732391000065],[117.25097593100008,37.737956715000053],[117.25096045200007,37.738142150000044],[117.25094070300008,37.73837870300008],[117.25083111100002,37.739691403000052],[117.25080204300002,37.740039503000048],[117.25062461000005,37.742164831000025],[117.25133038800004,37.742210640000053],[117.25234846700005,37.742268158000059],[117.25233010300008,37.742349925000042],[117.25228666500004,37.742543334000061],[117.25221244300008,37.742873835000069],[117.25258687600001,37.742911418000062],[117.25249232700003,37.743441819000054],[117.25307353500011,37.743519689000038],[117.2531786510001,37.742619182000055],[117.25317895600006,37.742616574000067],[117.25288867000006,37.742608579000034],[117.25291089900009,37.74238802900004]]]}}

When the first json object takes a value

It can be passed:

();

The form of acquisition;

When the second json object takes a value

Needs to be used first:

var a=eval('(' +  + ')');

is converted in the same way as the

(a);

to take values in the manner of

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.