I am currently in search of a reliable utility (such as Lodash) that can assist me in transforming deeply nested objects like the example shown below:
Source Data:
[{
"category": "blogs",
"results": {
"__type": "SearchProvider.SearchResultContainer",
"SearchResults": [{
"SearchId": null,
"MetaData": [{
"Name": "id",
"Value": "618",
"Other": "",
"Count": 0,
"Checked": true
}, {
"Name": "PostID",
"Value": "618",
"Other": "",
"Count": 0,
"Checked": true
}, {
"Name": "PublishDate",
"Value": "10/08/2012 6:28:00 AM",
"Other": "",
"Count": 0,
"Checked": true
}, {
"Name": "_version_",
"Value": 1571711408793452500,
"Other": "",
"Count": 0,
"Checked": true
}]
}, {
"SearchId": null,
"MetaData": [{
"Name": "id",
"Value": "605",
"Other": "",
"Count": 0,
"Checked": true
}, {
"Name": "PostID",
"Value": "605",
"Other": "",
"Count": 0,
"Checked": true
}, {
"Name": "PublishDate",
"Value": "03/01/2011 8:16:00 PM",
"Other": "",
"Count": 0,
"Checked": true
}, {
"Name": "_version_",
"Value": 1571711408284893200,
"Other": "",
"Count": 0,
"Checked": true
}]
}]
}
}]
The desired output is to extract the property names id
and PublishDate
, along with their corresponding values from the MetaData
array within each SearchResults
object:
Expected Data:
[
{
"id": "618",
"PublishDate": "10/08/2012 6:28:00 AM"
},
{
"id": "605",
"PublishDate": "03/01/2011 8:16:00 PM"
}
]
My attempt at using json-query did not yield the desired result. I found the syntax easy to understand, particularly with the help of an online testing tool (), but I was unable to generate the expected outcome.
This question was put on hold for not meeting the guidelines, being labeled as asking for a "recommendation" rather than an explicit solution. While I acknowledge the subjective nature of soliciting recommendations and potentially receiving a varied list of responses, my intention in seeking recommendations was different. I was looking for input from experienced JavaScript developers on various approaches (including those utilizing built-in ES6 features or external utility libraries like Lodash) to objectively achieve this specific outcome. Hence, there is an objective question posed here with an objectively identifiable answer. However, considering the multiple valid methodologies to attain the result, pinpointing a single answer may indeed pose challenges. This issue seems more reflective of a limitation within the Stack Overflow format, leading to the question being considered "off topic." If there exists a platform better suited for posting such inquiries seeking best practices and diverse, constructive responses (like those received thus far), I would gladly redirect my query there. In any case, both responses provided have enriched my understanding about the possible resolutions through different avenues. The response involving ES6 has enlightened me on leveraging its intrinsic capabilities (e.g., destructuring and spread operator) to accomplish the task, while the Lodash approach has shed light on achieving the same goal using that library. I am grateful to both respondents for their valuable insights - ideally, I'd acknowledge both answers for their contributions (though I comprehend why the question deviates from SO guidelines).
Considering my preference for using a "utility" like Lodash, I will opt for the Lodash solution (even though the ES6 explanation furthered my grasp of incorporating its native functionalities).