Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with nested JSON
#1
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:

{
"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"
       }
    ]
},

}

}
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):

{
    "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"
                            }
                         ]
                      }
                   ]
                }
             ]
          }
       }
    }
 }
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:

"""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))
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:

{
    "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"]
        ]}
        ]}
        ]}
        ]}
        ]}
        ]}
    ]  
    }
}
Reply
#2
It doesn't look like valid json data. Can you post data that can be loaded with json.loads() and the python code that you tried to update the data?
Reply
#3
(Dec-09-2021, 07:40 AM)Gribouillis Wrote: It doesn't look like valid json data. Can you post data that can be loaded with json.loads() and the python code that you tried to update the data?

Hi,

I added the real JSON example, where It's a valid json and also I put my code .
Reply
#4
Kalet Wrote:If for example appears:
"mode":"Spring",

It should be like that:
"Spring",
The result that you want is not clear because when you have "mode": "Spring" it means that "mode" and "Spring" are a pair (key, value) in a dictionary. But "Spring" alone is not a pair (key, value). Do you mean that you want to replace the dictionary by some other container having only values and no keys, like a list?

Can you write exactly by hand the structure of the expected python object? Forget about json for now, simply write a valid python object that is understood by the interpreter.
ndc85430 likes this post
Reply
#5
(Dec-09-2021, 01:03 PM)Gribouillis Wrote:
Kalet Wrote:If for example appears:
"mode":"Spring",

It should be like that:
"Spring",
The result that you want is not clear because when you have "mode": "Spring" it means that "mode" and "Spring" are a pair (key, value) in a dictionary. But "Spring" alone is not a pair (key, value). Do you mean that you want to replace the dictionary by some other container having only values and no keys, like a list?

Can you write exactly by hand the structure of the expected python object? Forget about json for now, simply write a valid python object that is understood by the interpreter.


Hi, I wrote the structure expected by my hands, I hope that It can be helpful for understand my problem, thanks!
Reply
#6
It is still unclear, for example "Cold" appears only once in the original data and three times in the expected data. How can the program generate the two missing "Cold" blocks? To generate any output, we need unambiguous rules.
Reply
#7
(Dec-09-2021, 08:31 PM)Gribouillis Wrote: It is still unclear, for example "Cold" appears only once in the original data and three times in the expected data. How can the program generate the two missing "Cold" blocks? To generate any output, we need unambiguous rules.

Spring or Cold or anything, I'm getting from a JSON of a webpage, so, I put those only for the example, but it can be Spring, Spring or Cold or anything... The problem is how to generate that json structure
Reply
#8
Kalet Wrote:The problem is how to generate that json structure
The problem is not to generate a json structure, it is to transform an existing structure into another one. In order to do this programmatically, you need to explain the rules to do that, at minimum show specific input data and the exact output data that is related with this input.

If we want to translate hieroglyphs into greek and we don't have a dictionary, we need at least a Rosetta stone.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Parse Nested JSON String in Python mmm07 4 1,429 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Read nested data from JSON - Getting an error marlonbown 5 1,310 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Convert nested sample json api data into csv in python shantanu97 3 2,728 May-21-2022, 01:30 PM
Last Post: deanhystad
  Convert python dataframe to nested json kat417 1 6,247 Mar-18-2022, 09:14 PM
Last Post: kat417
  Problem with Json Array kwekey 2 1,645 Aug-02-2021, 05:11 PM
Last Post: kwekey
  Problem to parse a json enigma619 3 2,342 Dec-04-2020, 08:16 AM
Last Post: enigma619
  JSON response from REST service get nested value nl2ttl 2 2,489 Nov-30-2020, 09:34 PM
Last Post: nl2ttl
  Nested function problem chipx 8 3,414 Oct-21-2020, 11:56 PM
Last Post: jefsummers
  JSON -> CSV conversion help! *I think Nested JSON* BrandonKastning 4 3,273 Apr-19-2020, 05:18 AM
Last Post: BrandonKastning
  json problem enigma619 9 3,616 Dec-19-2019, 08:29 AM
Last Post: enigma619

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020