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);
Related
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)
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"
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 working with facebook JS SDK which returns user's information in JSON format. I know how to get the response like response.email which returns email address. But how to get an element from a nested array object? Example: user's education history may contain multiple arrays and each array will have an element such as "name" of "school". I want to get the element from the last array of an object.
This is a sample JSON I got:-
"education": [
{
"school": {
"id": "162285817180560",
"name": "Jhenaidah** School"
},
"type": "H**hool",
"year": {
"id": "14404**5610606",
"name": "2011"
},
"id": "855**14449421"
},
{
"concentration": [
{
"id": "15158**968",
"name": "Sof**ering"
},
{
"id": "20179020**7859",
"name": "Dig**ty"
}
],
"school": {
"id": "10827**27428",
"name": "Univer**g"
},
"type": "College",
"id": "9885**826013"
},
{
"concentration": [
{
"id": "108196**810",
"name": "Science"
}
],
"school": {
"id": "2772**996993",
"name": "some COLLEGE NAME I WANT TO GET"
},
"type": "College",
"year": {
"id": "1388*****",
"name": "2013"
},
"id": "8811215**16"
}]
Let's say I want to get "name": "some COLLEGE NAME I WANT TO GET" from the last array. How to do that with Javascript? I hope I could explain my problem. Thank you
Here is a JsFiddle Example
var json = '{}' // your data;
// convert to javascript object:
var obj = JSON.parse(json);
// get last item in array:
var last = obj.education[obj.education.length - 1].school.name;
// result: some COLLEGE NAME I WANT TO GET
If your json above was saved to an object called json, you could access the school name "some COLLEGE NAME I WANT TO GET" with the following:
json.education[2].school.name
If you know where that element is, then you can just select it as already mentioned by calling
var obj = FACEBOOK_ACTION;
obj.education[2].school.name
If you want to select specifically the last element, then use something like this:
obj.education[ obj.education.length - 1 ].scool.name
Try this,
if (myData.hasOwnProperty('merchant_id')) {
// do something here
}
where JSON myData is:
{
amount: "10.00",
email: "someone#example.com",
merchant_id: "123",
mobile_no: "9874563210",
order_id: "123456",
passkey: "1234"
}
This is a simple example for your understanding. In your scenario of nested objects, loop over your JSON data and use hasOwnProperty to check if key name exists.
This question already has answers here:
Retrieve only the queried element in an object array in MongoDB collection
(18 answers)
Closed 7 years ago.
This is my document in MongoDB and I need to limit the output:
{
"_id": ObjectId("55880fb8c3addd201ee2f70e"),
"title": "Sales",
"level1": [{
"name": "Master",
"link": "/sales/master"
}, {
"name": "eCommerce",
"link": "/sales/ecommerce"
}]
}
My search query is:
db.collection.find({
title: "Sales",
"level1.name": "Master"
}, {
"title": 1,
"level1.name": 1,
"level1.name": "Master"
})
This is the expected output:
{
"_id": ObjectId("55880fb8c3addd201ee2f70e"),
"title": "Sales",
"level1": [{
"name": "Master",
"link": "/sales/master"
}]
}
You can use aggregation by $unwind level1 array to get expected result like following:
db.collection.aggregate({$unwind:"$level1"},{$match:{"level1.name":"Master","title" :"Sales"}})
If you want all under one array you can group it like :
db.collection.aggregate({$unwind:"$level1"},{$match:{"title" :"Sales","level1.name":"Master"}},{$group:{_id:"_id","title":{$first:"$title"},"level1":{$push:"$level1"}}})
Please try the below query :
db.collection.find({title :"Sales",
level1: {$elematch: {name" : "Master"}}},
{title : 1, level1.$ : 1});