This question already has an answer here:
Get length of a JavaScript array [duplicate]
(1 answer)
Closed 8 years ago.
I have the following data source -- converted to JSON using to XML using X2JS:
{
"blog": {
"article": [
{
"id": "1",
"author": "eat-sleep-code",
"title": {
"__cdata": "Thefirstarticle."
},
"content": {
"__cdata": "\nThisismyfirstarticleinmytestsite.\n"
},
"createdate": "2014-05-09"
},
{
"id": "2",
"author": "eat-sleep-code",
"title": {
"__cdata": "Thesecondarticle."
},
"content": {
"__cdata": "\nThisismysecondarticleinmytestsite.Thisarticle'screatedateisactuallyearlier.\n"
},
"createdate": "2014-05-08"
}
]
}
}
I am trying to find the number of "articles".
Object.keys(jsonObject).length; just gets me 1. I am guessing because it is finding one "blog" item.
jsonObject.blog.article.length
Related
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);
This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 years ago.
I'm working with a GeoJSON dataset that is formatted like this:
{
"type": "Feature",
"properties": {
"startcong": "109",
"district": "7",
"statename": "Pennsylvania",
"member": {
"112": {
"21168": {
"party": "Republican",
"name": "Meehan, Pat",
"district": "7"
}
},
"109": {
"15447": {
"party": "Republican",
"name": "Weldon, Curt", "district": "7"}
},
"110": {
"20744": {
"party": "Democrat",
"name": "Sestak, Joe",
"district": "7"
}
},
"111": {
"20744": {
"party": "Democrat",
"name": "Sestak, Joe",
"district": "7"
}
}
},
"endcong":
"112",
"id": "042109112007"
}
}
I'm struggling with how to access these nested objects. For instance, I can use feature.properties.member[112][21168] to access the party attribute. However:
That second numbered object ("21168") is not always consistently numbered.
That second numbered object is always the only nested object.
Is there a way to access that same party attribute using something like a wildcard? Ideally, something akin to feature.properties.member[112][*].party.
If the second number is the only nested object, you can find what the number is using the builtin Object.keys(obj), something like:
var obj = feature.properties.member[112],
key = Object.keys(obj)[0],
party = obj[key].party
Sadly there is no wildcards for property accessing, but you can find what the property names are fairly simply.
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});
This question already has answers here:
Finding matching objects in an array of objects?
(5 answers)
Closed 8 years ago.
Say I have a JSON array containing an bunch of different key/values per each object. I know how to use Javascript to target a specific key/value, but what if I want to search through the entire JSON file to find an item, and then also find a related pair within that same object.
For example, how would I scan the following JSON for "Moby Dick", but then also find the author that is tied to that title?
"store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
}
}
Assuming this is your object:
var store= {
"book": [
{...}, {...}
]
}
Then you can try to filter it like this:
var foundBooks = store.book.filter(function(book) { return book.title ==="Moby Dick"});
As correctly pointed out for #JLRiche foundBooks is an array. In order to access the first match it will be in the same way we do with all arrays:
var book = foundBooks[0];
You'd have to iterate the list, you can create some custom functions, like so:
function findTitle(title) {
for (var i = 0; i < data.store.book.length; i++) {
if (data.store.book[i].title == title) {
return data.store.book[i];
}
}
}
So you can do:
var book = findTitle("Moby Dick"),
author = book.author;
You would loop through your book objects and find where book.title === "Moby Dick" and then take a look at book.author for that particular object.
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
hello maybe stupid question but how can i print for example start out of this
var text = {
"responseData": {
"results": [
{
"GsearchResultClass": "GwebSearch",
"unescapedUrl": "http://dojotoolkit.org/",
"url": "http://dojotoolkit.org/",
"visibleUrl": "dojotoolkit.org",
"cacheUrl": "http://www.google.com/search?q=cache:sUiWYphwkaoJ:dojotoolkit.org",
"title": "Unbeatable JavaScript Tools - The <b>Dojo Toolkit</b>",
"titleNoFormatting": "Unbeatable JavaScript Tools - The Dojo Toolkit",
"content": "<b>Dojo</b> saves you time, delivers powerful performance, and scales with your development process. It's the <b>toolkit</b> experienced developers turn to for building <b>...</b>"
},
{
"GsearchResultClass": "GwebSearch",
"unescapedUrl": "http://dojotoolkit.org/download/",
"url": "http://dojotoolkit.org/download/",
"visibleUrl": "dojotoolkit.org",
"cacheUrl": "http://www.google.com/search?q=cache:cQhx_NOJhyYJ:dojotoolkit.org",
"title": "Download - The <b>Dojo Toolkit</b>",
"titleNoFormatting": "Download - The Dojo Toolkit",
"content": "This download is ideal for situations where a custom build will not be required."
},
{
"GsearchResultClass": "GwebSearch",
"unescapedUrl": "http://dojotoolkit.org/documentation/",
"url": "http://dojotoolkit.org/documentation/",
"visibleUrl": "dojotoolkit.org",
"cacheUrl": "http://www.google.com/search?q=cache:ws95YbyVgxgJ:dojotoolkit.org",
"title": "Documentation - The <b>Dojo Toolkit</b>",
"titleNoFormatting": "Documentation - The Dojo Toolkit",
"content": "How do I start learning Dojo? Where are the docs? How do I get support and <b>...</b>"
},
{
"GsearchResultClass": "GwebSearch",
"unescapedUrl": "http://en.wikipedia.org/wiki/Dojo_Toolkit",
"url": "http://en.wikipedia.org/wiki/Dojo_Toolkit",
"visibleUrl": "en.wikipedia.org",
"cacheUrl": "http://www.google.com/search?q=cache:6gxw4t2myDIJ:en.wikipedia.org",
"title": "<b>Dojo Toolkit</b> - Wikipedia, the free encyclopedia",
"titleNoFormatting": "Dojo Toolkit - Wikipedia, the free encyclopedia",
"content": "<b>Dojo Toolkit</b> (stylized as dōjō toolkit) is an open source modular JavaScript library (or more specifically JavaScript toolkit) designed to ease the rapid <b>...</b>"
}
],
"cursor": {
"resultCount": "83,500",
"pages": [
{
"start": "0",
"label": 1
},
{
"start": "4",
"label": 2
},
{
"start": "8",
"label": 3
},
{
"start": "12",
"label": 4
},
{
"start": "16",
"label": 5
},
{
"start": "20",
"label": 6
},
{
"start": "24",
"label": 7
},
{
"start": "28",
"label": 8
}
],
"estimatedResultCount": "83500",
"currentPageIndex": 0,
"moreResultsUrl": "http://www.google.com/search?oe=utf8&ie=utf8&source=uds&start=0&hl=en&q=dojo+toolkit",
"searchResultTime": "0.20"
}
},
"responseDetails": null,
"responseStatus": 200
}
I have problem with accessing object in object. Can u please provide me some information?
i wanted to do something like :
alert(text.responseData['results'])
Thank you
Paste this text json object in chrome's console. And try accessing text variable. It will auto complete and help you find the path to the objects inside the variable, you desire. That's what I do to json objects. For your start variable try this code.
var pages = text.responseData.cursor.pages;
for(var i=0; i < pages.length; i++){
console.log(pages[i].start)
}