1.3 json Follow python The difference between dictionaries in
Two 、json And python Interaction between
2.1 Python Handle json Module :json
2.2 example
2.2.1 Translate a dictionary into json strand
2.2.2 Python decode JSON object
2.2.3 Read json file
2.2.4 write in json file
2.3 Conversion correspondence
2.3.1 Python Type conversion JSON Type correspondence
2.3.2 json Type to Python Type comparison table of
3、 ... and 、 After reading json It can be interpreted as python Of DataFrame
One 、json Definition
1.1 Concept
JSON:JavaScript Object Notation 【JavaScript Object notation 】 JSON Is a lightweight data exchange format , Text formats that are completely independent of any programming language . commonly , The background application encapsulates the response data into JSON Format return .
1.2 Basic grammar
JSON The basic grammar is as follows :JSON The most common format is the key value pair of an object :key Can only be string, value It can be object、array、string、number、true/false、null.
As shown below : { “sites”: [ { “name”:“360” , “url”:“www.360.com” }, { “name”:“google” , “url”:“www.google.com” }, { “name”:“baidu” , “url”:“www.baidu.com” } ] } 1) The key is wrapped in double quotation marks , Followed by a colon “:”, And then the value of that key ; 2) The value can be a string 、 Numbers 、 Array and other data types ; 3) Objects are separated by commas ; 4)“{}” To save objects ; 5)“[]” Used to save arrays ;
1.3 json Follow python The difference between dictionaries in
1)json Of key It can only be a string ,dict Of key It can be anything hash The object of , for example : character string 、 Numbers 、 Tuples etc. ;
2) A dictionary is a data structure ,json Is a data format ; Dictionaries have many built-in functions , There are many ways to call , and json It's a format for data packaging , It's not as operational as a dictionary ;
3)json Force double quotation marks on the string of ,dict The string of can be in single quotes 、 Double quotes ;
generally speaking , We will reduce the json Turn into python Dictionaries or lists in , And then operate it .
Two 、json And python Interaction between
2.1 Python Handle json Module :json
Pythone3 The standard library of JSON modular , It's very convenient to help us with json Data conversion and processing , Here we mainly refer to serialization (json.dumps()、json.dump()) And deserialization (json.loads()、json.load()).
Serialization and deserialization : Convert objects to data formats that can be transferred over the network or stored on local disks ( Such as :XML、JSON Or a particular format of byte string ) This process is called serialization ; conversely , Is called deserialization .
frequently-used JSON Module method : 1)json.dumps(): take Python The object in is converted to JSON String object in ; 2)json.dump(): take python Object conversion to JSON String output to fp Streaming ; 3)json.loads(): take JSON The string object in is converted to Python Objects in the ; 4)json.load(): Read contains json Object file .
belt s It's all about strings , No s It's all about documents .
# Use paths instead of directly results["issues"]
pd.json_normalize(results, record_path="issues")[FIELDS]
# Custom delimiter : use "-" Replace the default "."
# Use sep Parameter to define the separator of nested structure connection
FIELDS = ["key", "fields-summary", "fields-issuetype-name", "fields-status-name", "fields-status-statusCategory-name"]
pd.json_normalize(results["issues"], sep = "-")[FIELDS]
# Control recursion
# If you don't want to recurs to every child object , have access to max_level Parameters control depth . under these circumstances , because statusCategory.name The field is located in JSON Object number 4 level , So it won't be included in the result DataFrame in .
# Only drill down to the second level of nesting
pd.json_normalize(results, record_path="issues", max_level = 2)