0
let newJson = {};

({
    "foo": {
        "name": newJson.FirstName,
        "height": newJson.RealHeight
    }
} =

{
    "foo": {
        "name": "Felipe",
        "height": "55"
    }
});
console.log({newJson})

As we Know the above code will return in JS the follow output:

{newJson :{FirstName: "Felipe", RealHeight: "55"}}

I Would like to kwnow if there is a Lib or a way to do it in PYTHON

1 Answers1

1

Searching for "desctructured assignment in Python" yields results.

You can use the native "tuple unpacking" as defined in PEP 448 :

json_data = {
    "foo": {
        "name": "Felipe",
        "height": "55"
    }
}

first_name, real_height = json_data["foo"]["name"], json_data["foo"]["height"]

print(first_name, real_height)
# Felipe 55

Or you can use something a bit closer which is based on the Python Data Model (inspired from this answer) :

from operator import itemgetter

json_data = {
    "foo": {
        "name": "Felipe",
        "height": "55"
    }
}

first_name, real_height = itemgetter("name", "height")(json_data["foo"])

print(first_name, real_height)
# Felipe 55
Lenormju
  • 4,078
  • 2
  • 8
  • 22