2

How do I iterate through a JSON string and replace every ObjectId into Unix Epoch time for further processing?

What I know: You get the first 8 chars from an objectId with:

subStrObjectId = objectId.substring(0, 8);

5668d142a54cc4960b55ea19 --> 5668D142

and convert these from hexadecimal into an Int value (epoch time in milliseconds):

subStrObjectIdInDec = parseInt(subStrObjectId, 16);

5668D142 (hex) --> 1449709890 (dec)

my Json string:

myJsonString = [
    [
        {"_id":"5668d142a54cc4960b55ea19","cid":10851045,"temp":25.4},
        {"_id":"5668d14ea54cc4960b55ea1a","cid":10850909,"temp":24.9}
    ],
    [
        {"_id":"5668d14fa54cc4960b55ea1b","cid":10851045,"hum":37.9},
        {"_id":"5668d3108c8cda92074b7ec9","cid":10850909,"hum":39.6}
    ],
    [
        {"_id":"5668d3198c8cda92074b7ecb","cid":10851045,"lux":34},
        {"_id":"5668d31e8c8cda92074b7ecc","cid":10850909,"lux":68}
    ]
];
4
  • what is your question? what you are doing seems ok. Commented Dec 25, 2015 at 3:36
  • how do I iterate through this json example string "myJsonString" and replace all the objectIds with the correct epoch time? how do I write such function? Commented Dec 25, 2015 at 3:52
  • I posted an answer, if that answers your question, consider marking it as the answer for future visitors. Commented Dec 25, 2015 at 4:13
  • 1
    Hello Amir and user3100115, both of you: thank you. Your solutions did solve my problem! @Amir, I'm from a different timezone, I had to go sleep. Patience Commented Dec 25, 2015 at 12:47

2 Answers 2

3

If you write what you know as a function:

function convert(id) {
  return parseInt(id.substring(0, 8), 16);
}

You can easily iterate over your objects and run your function on the objects.

I'd prefer functional-style javascript:

var data = myJsonString.map(function (array) {
  return array.map(function (item) {
    item.time = convert(item._id);
    return item;
  })
})

console.log(data);

But you can go iterative with loops as well:

for (var i=0;i<myJsonString.length;i++) {
  for (var j=0;j<myJsonString[i].length;j++) {
    myJsonString[i][j].time = convert(myJsonString[i][j]._id);
  }
}
console.log(myJsonString);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use the getTimestamp() to return the timestamp portion of the ObjectId() as a Date.

myJsonString.map(
    function(element) {
        return element.map(function(doc) {
            doc.timestamp = ObjectId(doc._id).getTimestamp(); 
            doc._id = ObjectId(doc._id); // Only if you want to convert your _id string to valid Objectid
            return doc;
        })
    }
);

Which yields:

[       
    [
            {
                    "_id" : ObjectId("5668d14fa54cc4960b55ea1b"),
                    "cid" : 10851045,
                    "hum" : 37.9,
                    "timestamp" : ISODate("2015-12-10T01:11:43Z")
            },
            {
                    "_id" : ObjectId("5668d3108c8cda92074b7ec9"),
                    "cid" : 10850909,
                    "hum" : 39.6,
                    "timestamp" : ISODate("2015-12-10T01:19:12Z")
            }
    ],
    [
            {
                    "_id" : ObjectId("5668d3198c8cda92074b7ecb"),
                    "cid" : 10851045,
                    "lux" : 34,
                    "timestamp" : ISODate("2015-12-10T01:19:21Z")
            },
            {
                    "_id" : ObjectId("5668d31e8c8cda92074b7ecc"),
                    "cid" : 10850909,
                    "lux" : 68,
                    "timestamp" : ISODate("2015-12-10T01:19:26Z")
            }
    ]
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.