I wrote a code in R but it can't take my heavy files, so I switched to Python. But I've been looking for 3 Python's equivalent functions of R and haven't found any satisfying results.
The 3 R functions are:
fromJSON()
unlist()
attr()
OK let me show you my problem, I show you what I do and what I get in R. And now I want to do the same thing in Python.
json_file <- '[
{
"id": "haha",
"type": "table",
"A": "HKD",
"B": "HKD",
"C": "HKD",
"V": "HKD",
"composition": [
{
"id": "AO",
"type": "panier"
},
{
"id": "KK",
"type": "basket",
"isAutoDiv": false,
"composition": [
{
"id": "600",
"type": "apple",
"number": 1.11
},
{
"id": "605",
"type": "peach",
"number": 1.79
}
]
},
{
"id": "KL",
"type": "basket"
}
]
},
{
"id": "hoho",
"type": "table",
"composition": [
{
"id": "KT",
"type": "panier"
},
{
"id": "OT",
"type": "panier"
},
{
"id": "CL",
"type": "basket",
"isAutoDiv": false,
"composition": [
{
"id": "450",
"type": "apple"
},
{
"id": "630",
"type": "orange"
},
{
"id": "023",
"type": "orange",
"composition": [
{
"id": "AOOOOOOO",
"type": "orangejuice"
},
{
"id": "VMVMVMVMV",
"type": "orangejuice"
}
]
}
]
}
]
}
]'
Now I do this: 1:
nestedjson <- fromJSON(json_file)
I can get:
[[1]]
[[1]]$id
[1] "haha"
[[1]]$type
[1] "table"
[[1]]$A
[1] "HKD"
[[1]]$B
[1] "HKD"
[[1]]$C
[1] "HKD"
[[1]]$V
[1] "HKD"
[[1]]$composition
[[1]]$composition[[1]]
[[1]]$composition[[1]]$id
[1] "AO"
[[1]]$composition[[1]]$type
[1] "panier"
[[1]]$composition[[2]]
[[1]]$composition[[2]]$id
[1] "KK"
[[1]]$composition[[2]]$type
[1] "basket"
[[1]]$composition[[2]]$isAutoDiv
[1] FALSE
[[1]]$composition[[2]]$composition
[[1]]$composition[[2]]$composition[[1]]
[[1]]$composition[[2]]$composition[[1]]$id
[1] "600"
[[1]]$composition[[2]]$composition[[1]]$type
[1] "apple"
[[1]]$composition[[2]]$composition[[1]]$number
[1] 1.11
[[1]]$composition[[2]]$composition[[2]]
[[1]]$composition[[2]]$composition[[2]]$id
[1] "605"
[[1]]$composition[[2]]$composition[[2]]$type
[1] "peach"
[[1]]$composition[[2]]$composition[[2]]$number
[1] 1.79
[[1]]$composition[[3]]
[[1]]$composition[[3]]$id
[1] "KL"
[[1]]$composition[[3]]$type
[1] "basket"
[[2]]
[[2]]$id
[1] "hoho"
[[2]]$type
[1] "table"
[[2]]$composition
[[2]]$composition[[1]]
[[2]]$composition[[1]]$id
[1] "KT"
[[2]]$composition[[1]]$type
[1] "panier"
[[2]]$composition[[2]]
[[2]]$composition[[2]]$id
[1] "OT"
[[2]]$composition[[2]]$type
[1] "panier"
[[2]]$composition[[3]]
[[2]]$composition[[3]]$id
[1] "CL"
[[2]]$composition[[3]]$type
[1] "basket"
[[2]]$composition[[3]]$isAutoDiv
[1] FALSE
[[2]]$composition[[3]]$composition
[[2]]$composition[[3]]$composition[[1]]
[[2]]$composition[[3]]$composition[[1]]$id
[1] "450"
[[2]]$composition[[3]]$composition[[1]]$type
[1] "apple"
[[2]]$composition[[3]]$composition[[2]]
[[2]]$composition[[3]]$composition[[2]]$id
[1] "630"
[[2]]$composition[[3]]$composition[[2]]$type
[1] "orange"
[[2]]$composition[[3]]$composition[[3]]
[[2]]$composition[[3]]$composition[[3]]$id
[1] "023"
[[2]]$composition[[3]]$composition[[3]]$type
[1] "orange"
[[2]]$composition[[3]]$composition[[3]]$composition
[[2]]$composition[[3]]$composition[[3]]$composition[[1]]
[[2]]$composition[[3]]$composition[[3]]$composition[[1]]$id
[1] "AOOOOOOO"
[[2]]$composition[[3]]$composition[[3]]$composition[[1]]$type
[1] "orangejuice"
[[2]]$composition[[3]]$composition[[3]]$composition[[2]]
[[2]]$composition[[3]]$composition[[3]]$composition[[2]]$id
[1] "VMVMVMVMV"
[[2]]$composition[[3]]$composition[[3]]$composition[[2]]$type
[1] "orangejuice"
Then I do this 2:
unnestedjson <- unlist(nestedjson)
I can get:
id
"haha"
type
"table"
A
"HKD"
B
"HKD"
C
"HKD"
V
"HKD"
composition.id
"AO"
composition.type
"panier"
composition.id
"KK"
composition.type
"basket"
composition.isAutoDiv
"FALSE"
composition.composition.id
"600"
composition.composition.type
"apple"
composition.composition.number
"1.11"
composition.composition.id
"605"
composition.composition.type
"peach"
composition.composition.number
"1.79"
composition.id
"KL"
composition.type
"basket"
id
"hoho"
type
"table"
composition.id
"KT"
composition.type
"panier"
composition.id
"OT"
composition.type
"panier"
composition.id
"CL"
composition.type
"basket"
composition.isAutoDiv
"FALSE"
composition.composition.id
"450"
composition.composition.type
"apple"
composition.composition.id
"630"
composition.composition.type
"orange"
composition.composition.id
"023"
composition.composition.type
"orange"
composition.composition.composition.id
"AOOOOOOO"
composition.composition.composition.type
"orangejuice"
composition.composition.composition.id
"VMVMVMVMV"
composition.composition.composition.type
"orangejuice"
Finally I do this 3:
unnestednames <- attr(unnestedjson, "names")
I can get the results below: And that is all I want from a Python code: A list of names in which the name of the attribute containing "composition." can show at which level the attribute is.
For example, an attribute at the 2nd nested level has the name "composition.type", an attribute at the 4th nested level has the name "composition.composition.composition.id".
[1] "id"
[2] "type"
[3] "A"
[4] "B"
[5] "C"
[6] "V"
[7] "composition.id"
[8] "composition.type"
[9] "composition.id"
[10] "composition.type"
[11] "composition.isAutoDiv"
[12] "composition.composition.id"
[13] "composition.composition.type"
[14] "composition.composition.number"
[15] "composition.composition.id"
[16] "composition.composition.type"
[17] "composition.composition.number"
[18] "composition.id"
[19] "composition.type"
[20] "id"
[21] "type"
[22] "composition.id"
[23] "composition.type"
[24] "composition.id"
[25] "composition.type"
[26] "composition.id"
[27] "composition.type"
[28] "composition.isAutoDiv"
[29] "composition.composition.id"
[30] "composition.composition.type"
[31] "composition.composition.id"
[32] "composition.composition.type"
[33] "composition.composition.id"
[34] "composition.composition.type"
[35] "composition.composition.composition.id"
[36] "composition.composition.composition.type"
[37] "composition.composition.composition.id"
[38] "composition.composition.composition.type"
I've been looking for some Python code but haven't found anything. If you have any idea to do these in Python, that would solve my big problem!
Many thanks!