How to access json object using node.js express.js - javascript

my json is like this
var jsonObj = JSON.stringify(arr, null, 4);
console.log(jsonObj);
my output like this
I want access each and every element in this json. Plese provide me a solution to do this task

From what I can tell it looks like you have an array of objects and within that object is a sub array ("subActs"). To be able to iterate over all the objects in the "subActs" array you need to iterate over it this way:
jsonObject.forEach(function(object) {
object.subActs.forEach(function(subObject) {
console.log(subObject);
});
});
This will iterate through the entire object and just print to the console the subActs array objects.

Related

Accessing Elements of JS Object

I have created array in a object,
var obj_report_dailog = { array_report_dailog : [] }
Then push data to object,
obj_report_dialog.array_report_dialog.push({from: fromDate})
obj_report_dialog.array_report_dialog.push({to: toDate})
obj_report_dialog.array_report_dialog.push({fabrika: fabrika})
Then,
var json = JSON.stringify(obj_report_dialog);
How can I access to elements of that object?
console.log("işte bu: " + json);
output:
işte bu: {"array_report_dialog":[{"from":"2017-08-01"},{"to":"2017-09-21"},{"fabrika":["Balçova"]}]}
Two things:
You don't want to JSON.stringify unless you're sending the resulting string somewhere that will parse it. Remember, JSON is a textual notation for data exchange; JSON.stringify gives you a string to send to some receiver. When you have the JSON string, you don't access the properties of the objects in it.
If you're receiving that JSON string, you'd parse it via JSON.parse and then access the properties on the result.
Leaving aside the JSON thing, you probably don't want to add data the way you're adding it. You're adding three separate objects as three entries in the array, each with one property. You probably want to push one object with all three properties:
obj_report_dialog.array_report_dialog.push({
from: fromDate,
to: toDate,
fabrika: fabrika
});
Then you'd access them as obj_report_dialog.array_report_dialog[0].from, obj_report_dialog.array_report_dialog[0].to, and obj_report_dialog.array_report_dialog[0].fabrika. Or more likely, you'd have a loop like this:
obj_report_dialog.array_report_dialog.forEach(function(entry) {
// Use entry.from, entry.to, and entry.fabrika here
});
(See this answer for more options for looping through arrays.)
But, if you really want to push them as separate objects, you'd access them as obj_report_dialog.array_report_dialog[0].from, obj_report_dialog.array_report_dialog[1].to, and obj_report_dialog.array_report_dialog[2].fabrika (note the indexes going up).
If you stringify a json, it's gonna create a string representation of your object.
To access data in a string like that we usually use JSON.parse that create an json object from a string. Which is the obj_report_dailog you had at start.
You can make object from json by using JSON.parse()
JSON.parse(json).array_report_dialog

How to retrieve data from json data

[{"id":7,"message":"This is another test message","taker_id":"131","giver_id":"102","status":"0","stamp":"2016-08-11"}]
That's my response. I try to get a datum. I have tried data.id but it fails and returns undefined.
As I assume that you are working with a JSON string, you first have to parse the string into and JSON object. Else you couldn't reach any of the properties.
parsedData = JSON.parse(data);
Then you can get your property:
parsedData[0].id
This seems to work just fine
var data = [{
"id":7,
"message":"This is another test message",
"taker_id":"131",
"giver_id":"102",
"status":"0",
"stamp":"2016-08-11"
}];
console.log(data[0].id);
https://jsbin.com/jewatakize/
if you just want to get the id from this one object then data[0].id will work just fine.
If you expect to have multiple objects in that same array then you can use a loop.
for example if this is angular you can do:
<div ng-repeat='info in data'>
<p>{{info.id}}</p>
</div>
This will allow you to iterate through multiple objects within the array and get all id's.
The problem here is that you have here an array of objects, and you are trying to access it without indexing.
You should first parse it using and then access the object by indexing
let objects = JSON.parse(data)
console.log(objects[0].id)

How to get id from array of Object?

I have data in response so here rcsaAssessmentData.data.lobCheckListDTOs is array , riskAsesCklstSessionKey is property of array , How can i get this key using filter or any other option.Below console is printing all objects but i just want value for the riskAsesCklstSessionKey.
So far tried code...
array.js
var opChecklist = rcsaAssessmentData.data.lobCheckListDTOs.filter(function(obj){
return obj.riskAsesCklstSessionKey;
console.log("opcheclist...............",obj.riskAsesCklstSessionKey);
});
$scope.challengesDTO.addChlngToChklst= obj.riskAsesCklstSessionKey;
This is what I think you mean:
You have an array of objects, rcsaAssessmentData.data.lobCheckListDTOs. Each of the objects in the array has a property called riskAsesCklstSessionKey. You are trying to get an array of those keys. If so, try this:
var keys = rcsaAssessmentData.data.lobCheckListDTOs.map(function(a) {return a.riskAsesCklstSessionKey;});

Convert array of objects to object

In my node REST application I have a function that queries a database for several records and returns an array of objects.
Since I want it to return a JSON object, I need a way to convert the array of objects to a single object with all the records inside.
Unfortunately I can't find an example on the internet about doing something like this.
Any help would be appreciated.
Why would you want to do that ? Its totally fine to JSON stringify an Array of items, you'll get a structure like
"[{},{},{},...]"
that is probably even an advantage, because you keep the order of items guaranteed.
See the object function of underscore.js.
Lets assume you have an array of objects with the form:
log {
name: "foo",
log: "bar"
}
Your could do:
var logs,//Array of logs
logObj = {}
for(i=0, i<logs.Length i++) {
logObj[logs[i].Name] = logs[i].log;
}
After the loop logObj should be:
logObj {
foo: bar,
nextName: cool comment,
etc.
}

Deleting particular data from a json object using JavaScript

I am using titanium for developing Android application. I want to delete some data from json object. My json object given below:
{"feeds":
[
{"username":"abc","user":"abc","feed":{"description":"dss","id":660,"user_id":1}},
{"username":"bcd","user":"bcd","feed":{"description":"dddd","id":659,"user_id":1}}
]
}
for receiving json object I used following code
var json = this.responseText;
var json = JSON.parse(json);
json.feeds.splice(0,1);
alert(json.feeds[0]);
I want to delete particular data from json object like json.feeds[0] using JavaScript. I am able to access json.feeds[0] but not able to delete. Is there any way to delete that data from json object using JavaScript?
You are using splice to properly remove an element from a javascript array:
json.feeds.splice(0,1)
Using the code you've provided it will look like:
(function(){
var json = {
"feeds": [
{"username":"abc","user":"abc","feed":{"description":"dss","id":660,"user_id":1}},
{"username":"bcd","user":"bcd","feed":{"description":"dddd","id":659,"user_id":1}}
]
};
json.feeds.splice(0,1);
console.log(json.feeds); // just to check that "feeds" contains only a single element
})();
Parse the JSON into a JavaScript data structure
Use splice to remove the elements you don't want from the array
Serialise the JavaScript objects back into JSON

Categories

Resources