I’m trying to insert some data from a proprietary JSON database into MongoDB for testing purposes. My data is currently in a json file in the format:
{ "_id": "213124123114",
"foo":bar",
"otherId": "2324242424",
...
}
To keep my test data relationships intact, I want to use sed to wrap all the _id and xxxId values with ObjectId(…)
My data would then look like:
{ "_id": ObjectId("213124123114"),
"foo":bar",
"otherId": ObjectId("2324242424"),
...
}
I would then take the data and insert it into mongo in the same format as displayed in the file.
I’m testing my regex in javascript, but the following assignment blows up:
var y = s/"_id":(\s?"[0-9]+"),/ObjectId($1)/gi
SyntaxError: Unexpected token :
Escaping the ‘:’ doesn’t seem to do anything.
When I remove the capture flag at the start, the regex assignment works as expected
var y = /"_id":(\s?"[0-9]+"),/
var p = "\"_id\": \"123123123121321212312\",";
y.test(p) === true
but I have no way to capture the value block I need to wrap.
Any ideas what I’m doing wrong?
Try this:
Here’s a fiddle: http://jsfiddle.net/7WJBm/1/