Bonjour

Je cherche à parser un JSON avec pas mal d'imbrication, qui me permetterais de récupérer les valeurs des champs :

csClientAddr, csClientPort, csServerAddr, csServerPort, ssServerAddr, ssServerPort

Pour l'instant je bosse sur un fichier, mais après se sera directement le retour de l'api

Mon JSON

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
{
    "entries": {
        "https://localhost/mgmt/tm/sys/connection/0": {
            "nestedStats": {
                "entries": {
                    "connectionId": {
                        "value": 0
                    },
                    "csBytesIn": {
                        "value": 800
                    },
                    "csBytesOut": {
                        "value": 800
                    },
                    "csClientAddr": {
                        "description": "xxx.xxx.77.146%1"
                    },
                    "csClientPort": {
                        "value": 57782
                    },
                    "csPacketsIn": {
                        "value": 8
                    },
                    "csPacketsOut": {
                        "value": 8
                    },
                    "csServerAddr": {
                        "description": "xxx.xxx.4.xxx%1"
                    },
                    "csServerPort": {
                        "value": 123
                    },
                    "flowAccelType": {
                        "description": "none"
                    },
                    "idleTime": {
                        "value": 31
                    },
                    "idleTimeout": {
                        "value": 60
                    },
                    "lasthopInfo": {
                        "description": "/Common/xx-xx-xx-xx-xxxxx-xxx xx:xx:xx73:bc:47"
                    },
                    "protocol": {
                        "value": 17
                    },
                    "slot": {
                        "description": "1"
                    },
                    "ssBytesIn": {
                        "value": 800
                    },
                    "ssBytesOut": {
                        "value": 800
                    },
                    "ssClientAddr": {
                        "description": "xxx.xxx.4.200%1"
                    },
                    "ssClientPort": {
                        "value": 57782
                    },
                    "ssPacketsIn": {
                        "value": 8
                    },
                    "ssPacketsOut": {
                        "value": 8
                    },
                    "ssServerAddr": {
                        "description": "xxx.xxx.4.2%1"
                    },
                    "ssServerPort": {
                        "value": 123
                    },
                    "tmm": {
                        "value": 1
                    },
                    "type": {
                        "description": "any"
                    },
                    "unitId": {
                        "value": 2
                    },
                    "virtualPathAddr": {
                        "description": "xxx.xxx.4.xxx%1"
                    },
                    "virtualPathPort": {
                        "value": 123
                    }
                }
            }
        },
		"https://localhost/mgmt/tm/sys/connection/999": {
            "nestedStats": {
                "entries": {
                    "connectionId": {
                        "value": 0
                    },
                    "csBytesIn": {
                        "value": 3136
                    },
                    "csBytesOut": {
                        "value": 88672
                    },
                    "csClientAddr": {
                        "description": "xxx.193.71.212%1"
                    },
                    "csClientPort": {
                        "value": 57528
                    },
                    "csPacketsIn": {
                        "value": 40
                    },
                    "csPacketsOut": {
                        "value": 96
                    },
                    "csServerAddr": {
                        "description": "xxx.xxx.4.246%1"
                    },
                    "csServerPort": {
                        "value": 8083
                    },
                    "flowAccelType": {
                        "description": "none"
                    },
                    "idleTime": {
                        "value": 189
                    },
                    "idleTimeout": {
                        "value": 300
                    },
                    "lasthopInfo": {
                        "description": "/Common/xx-xxx-xx-xx-xxxxx-xx xx:xx:xx:xx:xx:xx"
                    },
                    "protocol": {
                        "value": 6
                    },
                    "slot": {
                        "description": "1"
                    },
                    "ssBytesIn": {
                        "value": 12168
                    },
                    "ssBytesOut": {
                        "value": 2792
                    },
                    "ssClientAddr": {
                        "description": "xxx.193.71.212%1"
                    },
                    "ssClientPort": {
                        "value": 57528
                    },
                    "ssPacketsIn": {
                        "value": 32
                    },
                    "ssPacketsOut": {
                        "value": 40
                    },
                    "ssServerAddr": {
                        "description": "xxx.xxx.4.16%1"
                    },
                    "ssServerPort": {
                        "value": 8083
                    },
                    "tmm": {
                        "value": 1
                    },
                    "type": {
                        "description": "any"
                    },
                    "unitId": {
                        "value": 2
                    },
                    "virtualPathAddr": {
                        "description": "xxx.xxx.4.246%1"
                    },
                    "virtualPathPort": {
                        "value": 8083
                    }
                }
            }
        }
    },
    "kind": "tm:sys:connection:connectionstats",
    "selfLink": "https://localhost/mgmt/tm/sys/connection?ver=xx.1.2.6"
}

Mon code
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import json
import re
 
with open('f5-con.json') as json_data:
    data_dict = json.load(json_data)
    #print data_dict
    for n1 in data_dict["entries"]:
        #print n1
        for n2 in data_dict["entries"][n1]:
            #print n2
            for n3 in data_dict["entries"][n1][n2]:
                #print n3
                for n4 in data_dict["entries"][n1][n2][n3]:
                    #print n4
                    for n5 in data_dict["entries"][n1][n2][n3][n4]:
                                                #  in : 
                        filter = n1 + ">" + n4 + ">" + n5
                        if "csClientAddr" or "csClientPort" or "csServerAddr" or "csServerPort" or "ssServerAddr" or "ssServerPort" in filter:
                            print filter
                        for n6 in data_dict["entries"][n1][n2][n3][n4][n5]:
                            print n6
le print de filter est bon et je ne vois bien que les éléments que je souhaite

Quand je rajoute le for n6 ....
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
Exception has occurred: TypeError
'int' object is not iterable
  File "C:\Users\yd59035n\Documents\test_json\parse-json.py", line 21, in <module>
    for n6 in data_dict["entries"][n1][n2][n3][n4][n5]:
Je sèche un peut, quelqu'un aurais la solution ?

Mon approche est-elle bonne ?


merci d'avance