{
"RuntimeSources": {
"flask-webapp": {
"eb-flask1.3": {
"s3url": ""
}
}
},
"DeploymentId": 4,
"Serial": 4
}
There is such a json file. Now I need to extract the name of the key flask-webapp
, which is the string itself of flask-webapp
. How should I use it? Using Object.keys(), I get the three keys RuntimeSource, DeploymentId and Serial.
I feel that my description is a bit complicated. To summarize the problem is: how to extract the key in the next layer of key-value in the first key-value of this json file (it seems to be more complicated... I hope you can read it) Got it)
You can use python or javascript to implement it
This is the data that needs to be processed. The question of the subject should be regarded as a proper subset of the following questions
(The problem is that as long as we obtain
"flask-webapp"
)This process is temporarily called
flat
. I have also implemented this function here to obtain this solution.Use Object.keys to solve the problem
Object.keys
can obtain the enumerable first-level object property key namesUse features like this to write recursive functions:
ScreenShot
Specifically you need
flask-webapp
this key:Use JSON.stringify to solve the problem
JSON.stringify
can convert objects intoJSON string
For example
JSON.stringify(o)
you can get the result"{"RuntimeSources":{"flask-webapp":{"eb-flask1.3":{"s3url":""}}},"DeploymentId ":4,"Serial":4}"
Continue to observe and you will find:
Construct the JSON elements into an array, and then pick out the ones before the
colon
.Tool function
Implementation offlat
The above means:
The first reduce adds spaces before and after
{
}
:
,
Corresponding code
The result is this:
Original str from
"{"RuntimeSources":{"flask-webapp":{"eb-flask1.3":{"s3url":""}}},"DeploymentId":4,"Serial":4}"
After processing, it becomes
" { "RuntimeSources" : { "flask-webapp" : { "eb-flask1.3" : { "s3url" : "" } } } , "DeploymentId" : 4 , "Serial" : 4 } "
Get an intermediate result
A
Next
To be dealt with here
A
Corresponding code:
Convert
A
into intermediate arrayB
: (from string to array)The last reduce gets the result
Observe
B
and you can get a conclusionWrite the final reduce accordingly: collect the elements before the
colon
to get the resultScreenShot
The error "Cannot convert undefined or null to object" should be thrown at the end of execution. This is fine
If you want to implement it in python, dict is a hash table structure, which means that after the data is input, it has been hashed according to the characteristics and has its own order.
If you can specify the name of the key, you can still get it. If you can't specify it, you can still get it. The name of the key, then it can’t be done
Python’s default dictionary is unordered, but it can be implemented using the OrderedDict ordered dictionary.