Access values ​​in a JSON object

I have a JSON object event.bodythat contains {"article_url": "http://technewstt.com/bd1108/"}how I can access the value for article_url, in other words, I want to use a string http://technewstt.com/bd1108/.
I tried event.body.article_urland event.article_urland both of themundefined

+4
source share
3 answers

If you are sure that it event.bodycontains the correct data, you can parse it with json and get the value and key

JSON.parse('{"article_url": "http://technewstt.com/bd1108/"}', (key, value) => {
  console.log(key);  //logs the key which is article_url
  return value;      // returns the url value
})

which is equivalent

JSON.parse(JSON.stringify(event.body), (key, value) => {
  console.log(key);  
  return value;      
})

Another option, for example, a comment by @ Grégory NEUT, will return a URL value

JSON.parse(event.body).article_url  //returns the url value
0
source

The best way:

var url = JSON.parse(event.body).article_url;
0
source

body-parser :

const bodyParser = require('body-parser');

app.use(bodyParser.json());

.

-1

Source: https://habr.com/ru/post/1684903/


All Articles