MystereFire
MystereFire

Reputation: 25

node js get value from key

i need help i am trying to retrieve the value '802125586923257886' from my json file what can i do?

{
  "801502579829112852": {
    "channel": "802125586923257886"
  },
  "802163006698946570": {
    "channel": "802163007173951521"
  }
}

I tried this but it doesn't end

function jsonParser(stringValue) {
  var string = JSON.stringify(stringValue);
  var objectValue = JSON.parse(string);
}

console.log(jsonParser('801502579829112852'))

thank you in advance

Upvotes: 0

Views: 801

Answers (2)

M.S.Udhaya Raj
M.S.Udhaya Raj

Reputation: 150

// Your value is already in JSON format. No need to parse again
var json_dict = {
  "801502579829112852": {
    "channel": "802125586923257886"
  },
  "802163006698946570": {
    "channel": "802163007173951521"
  }
}

jsonParser(json_dict)

function jsonParser (pvalue){
  var chanldataobj = pvalue['801502579829112852']
  console.log(chanldataobj['channel'])
}

Upvotes: 1

Faraz M.
Faraz M.

Reputation: 73

What you are trying to do is lookup a value from a key which is in turn again the value to another key in a JSON dictionary using Javascript.

You need to save the JSON string into a variable and parse that into a Javascript Dictionary:

json_dict = `{
  "801502579829112852": {
    "channel": "802125586923257886"
  },
  "802163006698946570": {
    "channel": "802163007173951521"
  }
}`

js_dict = JSON.parse(json_dict)

Then, you can access the fields of your Javascript dictionary:

js_dict["801502579829112852"]["channel"]

which returns:

802125586923257886

Upvotes: 0

Related Questions