I have a large JSON object that may be irregular, and I am looking for a solution to flatten all of its objects into a single array.
Here is an example JSON:
{
"name":"getPost",
"request":{
"type":"object",
"fields":[{
"key":"id",
"value":{"type":"string"}
}]
},
"response":{
"type":"object",
"fields":[{
"key":"post",
"value":{
"type":"object",
"fields":[{
"key":"id",
"value":{"type":"string"}
},{
"key":"title",
"value":{"type":"string"}
},{
"key":"content",
"value":{"type":"string"}
},{
"key":"userId",
"value":{"type":"string"}
}]
}
}]
},
"error":[]
}
I would like the output to look something like this: (I only need objects with shallow key-value pairs)
[
{
"key":"id",
"value":{"type":"string"}
},{
"key":"post",
"value":{"type":"object"}
},{
"key":"id",
"value":{"type":"string"}
},{
"key":"title",
"value":{"type":"string"}
},{
"key":"content",
"value":{"type":"string"}
},{
"key":"userId",
"value":{"type":"string"}
}
]
I am wondering if there is a straightforward way to achieve this without using ES6 features.