Search code examples
javascriptnode.jsjsonsuperagent

How to extract value from GET/POST response message using superagent


I am using Node.js and superagent for testing my server implementation. Using Superagent GET request is send and POSITIVE response is received using below CODE. My usecase to get and log only "id" (OUTPUT) value from the response message. How to extract "id" value from the response.

message Format: json

My Superagent CODE:

request.get(MyURL).then(res res => { 

 data = JSON.parse(res.text);

console.log(data);})

OUTPUT:

{ data:
   [ { id: '35132df0-fa8c-42ec-80d7',
       name: 'name1',
       uri: '/xxxx/yyyy/35132df0-fa8c-42ec-80d7' } ],
  status: 'ok' 
}

Thank you


Solution

  • let foo = { data: [ { id: '35132df0-fa8c-42ec-80d7', name: 'name1', uri: '/xxxx/yyyy/35132df0-fa8c-42ec-80d7' } ], status: 'ok' }
    
    console.log(foo.data[0].id)