Hi,
I'm trying all the day and yesterday for two things:
1) I want to modify a lot of values from a JSON object in Python with the following structure:
And you can see that "mode" is always in the json(obviously, there are more keys inside of that but for the example I think that is good). I need to put thr value of mode and replace in the mode key:
If for example appears:
"mode":"Spring",
It should be like that:
"Spring",
But I tried a lot an nothing, because I don't know to do this recursively or I don't know :/. I tried acummulating with json.loads(jsonobject)["Moderators"]["temperature"]["season"] and so on, but just I'm getting one, not all of the attributes 😐.
And the another part, is replace quality atributes like speed and manage, I want to put my own values, like:
"quality"{
"speed":"fast",
"manage":"xxxxf"
}
I want replace like:
"quantity"{
"quantity1":["xxxx","dddd","zzz"],
"quantity2":["xxxx","dddd","zzz"],
},
It's for all that appears "quality".
Thank you and sorry for the format of all, I'm with mobile data!
EDIT: Following is the real JSON example(with the JSON that I have the problem):
So, I want to change "season":"Spring", by "Spring", and also, quality like the next:
"quality"{
"speed":"fast",
"manage":"xxxxf"
}
I want replace like:
"quantity": [{
"quantity1":["xxxx","dddd","zzz"],
"quantity2":["xxxx","dddd","zzz"]
]},
My python code:
But I want the things as I told to you, the problem is that right now I don't know how to do that :/.
Expected JSON:
I'm trying all the day and yesterday for two things:
1) I want to modify a lot of values from a JSON object in Python with the following structure:
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 |
{ "Moderators" :{ ... "something" {... }, "temperature" { "season" :{ "mode" : "Spring" , "quality" { "speed" : "fast" "manage" : "xxxxf" }, "season" :{ "mode" : "Spring" , "quality" { "speed" : "fast" "manage" : "xxxxf" }, "season" :[ { "mode" : "Spring" , "quality" { "speed" : "fast" "manage" : "xxxxf" }, { "mode" : "Spring" , "speed" : "fast" "manage" : "xxxxf" } ] }, { "mode" : "Cold" "quality" { "speed" : "fast" "manage" : "xxxxf" }, { "mode" : "Spring" "quality" { "speed" : "fast" "manage" : "xxxxf" } ] }, } } |
If for example appears:
"mode":"Spring",
It should be like that:
"Spring",
But I tried a lot an nothing, because I don't know to do this recursively or I don't know :/. I tried acummulating with json.loads(jsonobject)["Moderators"]["temperature"]["season"] and so on, but just I'm getting one, not all of the attributes 😐.
And the another part, is replace quality atributes like speed and manage, I want to put my own values, like:
"quality"{
"speed":"fast",
"manage":"xxxxf"
}
I want replace like:
"quantity"{
"quantity1":["xxxx","dddd","zzz"],
"quantity2":["xxxx","dddd","zzz"],
},
It's for all that appears "quality".
Thank you and sorry for the format of all, I'm with mobile data!
EDIT: Following is the real JSON example(with the JSON that I have the problem):
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 |
{ "moderators" :{ "student" : "bachelor" , "id" : "2021" , "school" : "641" , "qualities" :{ "season" : "Spring" , "quality" :{ "speed" : "Fast xxxxxxxx" , "manage" : "Any text xxxxxxx" }, "qualities" :{ "season" : "Spring" , "quality" :{ "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxx" }, "qualities" :[ { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Cold" , "qualities" :[ { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Spring AND" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] } ] } ] } } } } |
"quality"{
"speed":"fast",
"manage":"xxxxf"
}
I want replace like:
"quantity": [{
"quantity1":["xxxx","dddd","zzz"],
"quantity2":["xxxx","dddd","zzz"]
]},
My python code:
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
"""Extract nested values from a JSON tree.""" def flatten_json(obj): ret = {} def flatten(x, flattened_key = ""): if type (x) is dict : for current_key in x: flatten(x[current_key], flattened_key + current_key + '_' ) elif type (x) is list : i = 0 for elem in x: flatten(elem, flattened_key + str (i) + '_' ) i + = 1 else : ret[flattened_key[: - 1 ]] = x flatten(obj) return ret if __name__ = = "__main__" : nested_obj = obj = { "moderators" :{ "student" : "bachelor" , "id" : "2021" , "school" : "641" , "qualities" :{ "season" : "Spring" , "quality" :{ "speed" : "Fast xxxxxxxx" , "manage" : "Any text xxxxxxx" }, "qualities" :{ "season" : "Spring" , "quality" :{ "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxx" }, "qualities" :[ { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Cold" , "qualities" :[ { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Spring AND" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] }, { "season" : "Spring" , "quality" :[ { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" }, { "speed" : "Slow xxxxxxxx" , "manage" : "Any text xxxxxxx" } ] } ] } ] } } } } print (flatten_json(nested_obj)) |
Expected JSON:
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 |
{ "moderators" :{ "student" : "bachelor" , "id" : "2021" , "school" : "641" , "qualities" :[ { "Spring" : [ { "Cold" : [ [ "data1" , "data2" , "data3" , "data4" ], { "Spring" : [ { "Spring" : [ [ "txt1" , "txt2" , "txt3" ], [ "abc" , "txt2" , "txt3" ], [ "azx" , "txt2" , "txt3" ] ]}, { "Cold" : [ [ "txt1" , "txt2" , "txt3" ], [ "abc" , "txt2" , "txt3" ], [ "azx" , "txt2" , "txt3" ] ]}, { "Spring" : [ [ "txt1" , "txt2" , "txt3" ], [ "abc" , "txt2" , "txt3" ], [ "azx" , "txt2" , "txt3" ] ]} ]} ]}, { "Cold" : [ [ "data1" , "data2" , "data3" , "data4" ], { "Spring" : [ { "Spring" : [ [ "txt1" , "txt2" , "txt3" ], [ "abc" , "txt2" , "txt3" ], [ "azx" , "txt2" , "txt3" ] ]}, { "Spring" : [ [ "data1" , "data2" , "data3" , "data4" ], { "Spring" : [ { "Spring" : [ [ "txt1" , "txt2" , "txt3" ], [ "abc" , "txt2" , "txt3" ], [ "azx" , "txt2" , "txt3" ] ]} ]} ]} ]} ]} ]} ] } } |