python json format parameters through all key, value and the value for the replacement key

1. interfaces for automated testing, generic interface to send json return form, we often need to traverse the value json file in all key, value and modify the replacement for the key.

Json e.g. transmission / reception of file:

SendRegisterVerificationCodejson_txt = """
{
  "header":{
    "funcNo": "IF010002",
    "opStation": "11.11.1.1",
    "appId": "aaaaaa",
    "deviceId": "kk",
    "ver":"wx-1.0",
    "channel": "4"
  },
  "payload": {
    "mobileTel": "13817120001"
  }
}
"""

Need to find all the packets key, value, this is json dictionary contains dictionary form, such as the dictionary traversal, still containing a key value corresponding to the value of a dictionary, the need to continue to traverse this case need to be processed recursively

code show as below:

 1 import json
 2 #json文件发送形式
 3 SendRegisterVerificationCodejson_txt = """
 4 {
 5   "header":{
 6     "funcNo": "IF010002",
 7     "opStation": "11.11.1.1",
 8     "appId": "aaaaaa",
 9     "deviceId": "kk",
10     "ver":"wx-1.0",
11     "channel": "4"
12   },
13   "payload": {
14     "mobileTel": "13817120001"
15   }
16 }
17 """
18 date_json =json.loads (SendRegisterVerificationCodejson_txt)
 19  Print (date_json)
 20  Print ( " * " * 10 )
 21  # to send, every time you need to register a new phone number, you need to json each prompt mobileTel the value transmitted 
22  # traversing json file All the key corresponding to the value 
23 is DIC = {}
 24  DEF json_txt (dic_json):
 25      iF isinstance (dic_json, dict): # determines whether dictionary type isinstance returns True to false      
26 is          for key in dic_json:
 27              iF isinstance (dic_json [key ], dict): # If dic_json [key] is still a dictionary type
28                 print("****key--:%s value--: %s"%(key,dic_json[key]))
29                 json_txt(dic_json[key])
30                 dic[key] = dic_json[key]
31             else:
32                 print("****key--:%s value--: %s"%(key,dic_json[key]))
33                 dic[key] = dic_json[key]
34                 
35 json_txt(date_json)
36 print("dic ---: "+str(dic))

Output:

data_json:  {'header': {'funcNo': 'IF010002', 'opStation': '11.11.1.1', 'appId': 'aaaaaa', 'deviceId': 'kk', 'ver': 'wx-1.0', 'channel': '4'}, 'payload': {'mobileTel': '13817120001'}}
data_json:  {'header': {'funcNo': 'IF010002', 'opStation': '11.11.1.1', 'appId': 'aaaaaa', 'deviceId': 'kk', 'ver': 'wx-1.0', 'channel': '4'}, 'payload': {'mobileTel': '13817120001'}}
**********
****key--:header ,value--: {'funcNo': 'IF010002', 'opStation': '11.11.1.1', 'appId': 'aaaaaa', 'deviceId': 'kk', 'ver': 'wx-1.0', 'channel': '4'}
****key--:funcNo ,value--: IF010002
****key--:opStation ,value--: 11.11.1.1
****key--:appId ,value--: aaaaaa
****key--:deviceId ,value--: kk
****key--:ver ,value--: wx-1.0
****key--:channel ,value--: 4
****key--:payload ,value--: {'mobileTel': '13817120001'}
****key--:mobileTel ,value--: 13817120001
dic ---: {'funcNo': 'IF010002', 'opStation': '11.11.1.1', 'appId': 'aaaaaa', 'deviceId': 'kk', 'ver': 'wx-1.0', 'channel': '4', 'header': {'funcNo': 'IF010002', 'opStation': '11.11.1.1', 'appId': 'aaaaaa', 'deviceId': 'kk', 'ver': 'wx-1.0', 'channel': '4'}, 'mobileTel': '13817120001', 'payload': {'mobileTel': '13817120001'}}

 

2. For interface testing, json main content unchanged, in general we are modifying a value in the json key value for the test, this interface is the mobile phone number registered interface, we are all normal circumstances modify the phone number for testing;

As will Key - ' mobileTel ' for the Value - ' 13,817,120,001 ' modification, if the modification is 13333333333;

After that we need to traverse json dictionary key value, than if the key is to traverse to modify its value.

 def  check_json_value(dic_json,k,v):
    if isinstance(dic_json,dict):
        for key in dic_json:
            if key == k:
                dic_json[key] = v
            elif isinstance(dic_json[key],dict):
                check_json_value(dic_json[key],k,v)
print("date_json 变更前   :")
print(date_json)
check_json_value(date_json,'mobileTel','13333333333')
print("date_json 变更后   :")
print(date_json)

Complete code:

. 1  Import JSON
 2  "" " 
. 3  achieved:
 . 4  1.json_txt (dic_json): JSON format traversal key, value, stored in the dictionary
 . 5  2.heck_json_value (dic_json, K, V) JSON format, after the traversal, the replacement key value value
 . 6  3.data_Json (Sendjson_txt), the string into json json format
 . 7  "" " 
. 8  
. 9  # json json serialization format 
10  DEF data_Json (Sendjson_txt):
 . 11      data_json = json.loads (Sendjson_txt)
 12 is      Print ( ' data_json: ' , data_json)
 13 is      return data_json
 14  
15  #Traverse json files for all the key corresponding to the value, stored in a dictionary 
16 DIC = {}
 . 17  DEF json_txt (dic_json):
 18 is      IF isinstance (dic_json, dict): # determines whether dictionary type isinstance returns True, to false      
. 19          for key in dic_json:
 20              IF isinstance (dic_json [key], dict): # If dic_json [key] is still a dictionary type 
21                  Print ( " **** Key -:% S, Value -:% S " % (Key , dic_json [Key]))
 22 is                  # recursive call 
23 is                  json_txt (dic_json [Key])
 24                  DIC [Key] = dic_json[key]
25             else:
26                 print("****key--:%s ,value--: %s"%(key,dic_json[key]))
27                 dic[key] = dic_json[key]
28     
29 
30 #遍历json字典key值后,查到ke则修改值value
31 def  check_json_value(dic_json,k,v):
32     if isinstance(dic_json,dict):
33         for key in dic_json:
34             if key == k:
35                 dic_json[key] = v
36             elif isinstance(dic_json[key],dict):
37                 check_json_value(dic_json[key],k,v)
38     
39 if __name__=="__main__":
40     #json文件发送形式
41     Sendjson_txt = """
42     {
43       "header":{
44         "funcNo": "IF010002",
45         "opStation": "11.11.1.1",
46         "appId": "aaaaaa",
47         "deviceId": "kk",
48         "ver":"wx-1.0",
49         "channel": "4"
50       },
51       "payload": {
52         "mobileTel": "13817120001"
53       }
54     }
55     """
56     #格式化
57     data_json = data_Json(Sendjson_txt)
58     print('data_json: ',data_json)
59     print("*"*10)
60     #
61     json_txt(data_json)
62     print("dic ---: "+str(dic))
63 
64     #
65     print("data_json 变更前   :")
66     print(data_json)
67     check_json_value(data_json,'mobileTel','13333333333')
68     print("data_json 变更后   :")
69     print(data_json)

Results of the:

data_json:  {'header': {'funcNo': 'IF010002', 'opStation': '11.11.1.1', 'appId': 'aaaaaa', 'deviceId': 'kk', 'ver': 'wx-1.0', 'channel': '4'}, 'payload': {'mobileTel': '13817120001'}}
data_json:  {'header': {'funcNo': 'IF010002', 'opStation': '11.11.1.1', 'appId': 'aaaaaa', 'deviceId': 'kk', 'ver': 'wx-1.0', 'channel': '4'}, 'payload': {'mobileTel': '13817120001'}}
**********
****key--:header ,value--: {'funcNo': 'IF010002', 'opStation': '11.11.1.1', 'appId': 'aaaaaa', 'deviceId': 'kk', 'ver': 'wx-1.0', 'channel': '4'}
****key--:funcNo ,value--: IF010002
****key--:opStation ,value--: 11.11.1.1
****key--:appId ,value--: aaaaaa
****key--:deviceId ,value--: kk
****key--:ver ,value--: wx-1.0
****key--:channel ,value--: 4
****key--:payload ,value--: {'mobileTel': '13817120001'}
****key--:mobileTel ,value--: 13817120001
dic ---: {'funcNo': 'IF010002', 'opStation': '11.11.1.1', 'appId': 'aaaaaa', 'deviceId': 'kk', 'ver': 'wx-1.0', 'channel': '4', 'header': {'funcNo': 'IF010002', 'opStation': '11.11.1.1', 'appId': 'aaaaaa', 'deviceId': 'kk', 'ver': 'wx-1.0', 'channel': '4'}, 'mobileTel': '13817120001', 'payload': {'mobileTel': '13817120001'}}
data_json 变更前   :
{'header': {'funcNo': 'IF010002', 'opStation': '11.11.1.1', 'for appId ':' AAAAAA ',' deviceId ':' KK ',' Ver ':' WX-1.0 ',' Channel ':'. 4 '},' payload ': {' mobileTel ':' 13,817,120,001 '}}
data_json change Rear :
{'header': {'funcNo': 'IF010002', 'opStation': '11.11.1.1', 'appId': 'aaaaaa', 'deviceId': 'kk', 'ver': 'wx-1.0', 'channel': '4'}, 'payload': {'mobileTel': '13333333333'}}

---------------------------------------------------------------------------------------------------------------------------------------------------------------

PS: writing a logical interface handle, thanks to the original author's share: https: //www.cnblogs.com/HZQHZA/p/7301362.html

 

Guess you like

Origin www.cnblogs.com/lisa2016/p/11802939.html