Get value from object/array [duplicate] - javascript

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 1 year ago.
I need to get the token only in a variable in JS.
{
"user": {
"id": "string",
"nickName": "string",
"score": 0,
"rank": 0
},
"token": "string"
}
This is my response saved in a variable but I only need to get the "String" value from the token

If you have stored this response in a variable E.g.:
let response = {
"user": {
"id": "string",
"nickName": "string",
"score": 0,
"rank": 0
},
"token": "string"
}
You can extract the value from the "token" property like this
let tokenFromObject = response.token
or
let tokenFromObject = response["token"]
or
let { token } = response

You should be able to take the variable you got this json in and do this:
let a = {"user":{},"token": "string"}; // this is what you got
console.log(a["token"]) // a["token"] will give you "string"

Related

How to create a key value pair from two values [duplicate]

This question already has answers here:
How to convert an array into an object in javascript with mapped key-value pairs?
(4 answers)
Closed 7 months ago.
I'm returning key-value fields from the database, how do merge the two values into a new key-value pair?
{
"conditions": [{
"key": "target_condition",
"value": "100"
},
{
"key": "target_count_or_value",
"value": "value"
}
]
}
How to:
{
"conditions": {
"target_condition": "110",
"target_count_or_value": "quantity" }
}
data.conditions = data.conditions.map(item=>{[item.key]: item.value})
this uses the map function and dynamic object keys to build new objects

How do I convert an object of objects to array of objects [duplicate]

This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 8 months ago.
I am fetching an API endpoint which returns data in the format
{
"0": {
"name": "Rohan"
},
"1": {
"name": "Meghan"
},
"2": {
"name": "Rita"
}
}
But since it is not array my map function throws an error
Home.jsx:39 Uncaught TypeError: users.map is not a function
Hence I want to convert this object of objects into a const arrOfarr. The array should look like:
[
{
"name": "Rohan"
},
{
"name": "Meghan"
},
{
"name": "Rita"
}
]
You can get key and value pairs with this line.
Object.entries(yourObj)

How to get a specific value from json [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Safely turning a JSON string into an object
(28 answers)
Closed 1 year ago.
How to retrieve value from json using webload javascript
Message: keys value is : {"_Success": 1, "Criteria": "1234", "SearchAndSelect": [ {"DateOfBirth":
"15/02/1962","EmpNo": "123456","LastName": "John", "FirstName": "Smith", "Reference": "1234", "Keys":
"5eac0bbd-82d7-4959-8496-2cdb13dea292" } ] }
I want to retrieve the value of Keys
"Keys": "5eac0bbd-82d7-4959-8496-2cdb13dea292"
var jsonParsed = JSON.parse(json);
var keys = jsonParsed.SearchAndSelect[0].Keys;
console.log(keys); // "5eac0bbd-82d7-4959-8496-2cdb13dea292"
SearchAndSelect is the Key of the JSON which you should search in. It is a List of JSONS
so in case you are using javascript you would do something like this
let data = JSON.parse(result)
for(let element of data.SearchAndSelect){
console.log(element.Keys)
}
This handles in case there are multiple values in SearchAndSelect Array
var data = {"_Success": 1, "Criteria": "1234", "SearchAndSelect": [ {"DateOfBirth":
"15/02/1962","EmpNo": "123456","LastName": "John", "FirstName": "Smith", "Reference": "1234", "Keys":
"5eac0bbd-82d7-4959-8496-2cdb13dea292" } ] };
for (const property of data['SearchAndSelect']) {
console.log(property['Keys']);
}
Use :
JSON.parse(`{"_Success": 1, "Criteria": "1234", "SearchAndSelect": [ {"DateOfBirth":
"15/02/1962","EmpNo": "123456","LastName": "John", "FirstName": "Smith", "Reference": "1234", "Keys":
"5eac0bbd-82d7-4959-8496-2cdb13dea292" } ] }`)

I am trying to find a specific object in an array using node.js [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
My array is:
[
{
"_attributes": {
"key": "attributes"
},
"dt_assoc": {
"item": {
"_attributes": {
"key": "status"
},
"_text": "taken"
}
}
}
]
I have tried a couple of options but can't seem to get a specific object. Im trying to get; "dt_assoc":
{
"item": {
"_attributes": {
"key": "status"
},
"_text": "taken"
};
That's an array with only one item. Counting starts at zero. So to get to the item, you'd use array[0].
That will get you the object. That object has a property named dt_assoc. To access the property, you'd use array[0].dt_assoc.
You access it by specifying index as 0 because it is an array.
const data = [{
"_attributes": {
"key": "attributes"
},
"dt_assoc": {
"item": {
"_attributes": {
"key": "status"
},
"_text": "taken"
}
}
}]
console.log(data[0].dt_assoc)

see undefined when try to parse json [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
I have tried to parse this JSON file. But I see undefined.
I need to receive only value, where the key equals level1.
[{
"id": 2,
"name": "Peter",
"products": [{
"title": "first",
"price": 100
},
{
"title": "second",
"price": 200,
"desciption": [{
"level1": "good",
"level2": "bad"
},
{
"level3": "super",
"level4": "hell"
}
]
}
],
"country": "USA"
}]
const fs = require('fs');
let file = fs.readFileSync("./file.json");
let parsed = JSON.parse(file);
console.log(parsed["name"])
console.log(parsed.name);
and I see in the conlose "undefined"
Your JSON data represents an array of objects. If after parsing you want the property "name" of the first element, it's:
console.log(parsed[0]["name"])
or
console.log(parsed[0].name);

Categories