-1

I have a string "{\"id\":\"35112914\"}" obtained after JSON.stringify . I want just the number 35112914 or the first instance of a number ,i.e. 3 to the last ,i.e., 4 . Is there any other way of approaching this ?

Edit: I had to stringify because of a weird error I was getting. While accessing the property of the object, i was getting undefined as the answer , which made the number inaccessible. Stringify atleast shows up the number .

Edit 2 :

My code is written in node which makes curl request to a REST api .

curl.request(optionsRun, function (error, response) {
                console.log(response);
                setTimeout(function () {
                    ID = response ;
                    console.log(ID["id"]) ;
                },2000) ;
            }) ;

Output:

{"id":"35113281"}
undefined
saruftw
  • 1,104
  • 2
  • 14
  • 37

2 Answers2

4

You need to parse your response to a JS object before you can access it using JS object methods:

curl.request(optionsRun, function (error, response) {
  var ID = JSON.parse(response);
  setTimeout(function () {
    console.log(ID.id) ;
  }, 2000);
});

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95
0

this regexp will give you any numbers combination between two quotes /\"(\d+)\"/

Katya Pavlenko
  • 3,303
  • 3
  • 16
  • 28
  • 1
    The OP has an error in his code that sound like it's easily fixable so you might find this answer is a little premature. – Andy Jun 04 '15 at 14:12