I am trying to convert an existing json string
field to json array/object
as I have recently moved data from mysql
to mongodb
.
{
"_id": {
"$oid": "63f241012a9551202e909257"
},
"title": "Jumanji: Welcome To The Jungle",
"description": "...",
...
"info": "[{"year": "2017", ... },{"year": "2019", ... }]",
...
}
I need this to be
{
"_id": {
"$oid": "63f241012a9551202e909257"
},
"title": "Jumanji: Welcome To The Jungle",
"description": "...",
...
"info": [{
"year": 2017,
...
}, {
"year": 2019,
...
}],
...
}
2
Answers
With reference to @rickhg12hs solution, below is another way to perform the same task.
Also, please keep that in mind that the
arrow function syntax
is not supported in this scenario. So, always usefunction
notations to perform such operations.Here’s one way to convert your JSON string by letting Javascript
parse
it.Try it on mongoplayground.net.