I need some help for a json value. I data is -
[
{
"statusCode":200,
"body":{
"token":"xxxxx"
},
"headers":{
"date":"Thu, 28 Jul 2016 11:03:17 GMT",
"server":"Apache/2.2.15 (CentOS)",
"x-powered-by":"PHP/5.6.22",
"cache-control":"private, must-revalidate",
"etag":"\"9517ef72d528ad7a3bc04c64d1cc1cc9\"",
"set-cookie":[
"XSRF-TOKEN=xxx; expires=Thu, 28-Jul-2016 13:03:17 GMT; Max-Age=7200; path=/",
"laravel_session=eyJpdiI6IkZITXdyTGtpZlRkc1hmQkptUWpZSEE9PSIsInZhbHVlIjoicGxLUmJxRzlcL2dGTTdVcVJiQ1g2QTh4enQxdDI5NElCbGJkVllKYVR0MG1LQTljaFhhUFJSUVVXTytheUxqajZjV3FVUkh2SUhPK0ZtelhIQjcxVk5nPT0iLCJtYWMiOiJkOTg1MWFiYjY5ZTdhNThkODk5N2Y1MmRlOWEwZWMwYWQ4MGE4ZDVjMWRjMGMwNjA0MTlmNjQ1YzNmNDM3NWVkIn0%3D; expires=Thu, 28-Jul-2016 13:03:17 GMT; Max-Age=7200; path=/; httponly"
],
"vary":"Accept-Encoding",
"content-length":"305",
"connection":"close",
"content-type":"application/json"
},
"request":{
"uri":{
"protocol":"http:",
"slashes":true,
"auth":null,
"host":"api.shobkichhu.com",
"port":80,
"hostname":"api.shobkichhu.com",
"hash":null,
"search":null,
"query":null,
"pathname":"/api/auth/login",
"path":"/api/auth/login",
"href":"http://api.shobkichhu.com/api/auth/login"
},
"method":"POST",
"headers":{
"content-type":"application/json",
"accept":"application/json",
"content-length":45
}
}
},
{
"token":"xxxx"
}
]
I need to access token. How can I access token ?
Thanks in advance
This is the solution
var data =[
{
"statusCode":200,
"body":{
"token":"xxxxx"
},
},
{
"token":"xxxx"
}
];
console.log(data[0].body.token); // for first token
console.log(data[1].token); // for second token
var myObj = [
{
body:
{
token : 'xxxxxxx'
}
},
{
other : 'aaa'
}
];
console.log(myObj[0].body.token);
//or other version
var bodyObj = myObj.find(function(item){ return item && item.body});
console.log(bodyObj.body.token);
Assuming you have parsed the JSON (and that it's valid JSON, which it isn't in the question) and assigned the result to data, you access it like this:
data[0].body.token
...because:
data refers to an array
[0] accesses the first entry in the array, which refers to an object
.body accesses that object's body property value, which refers to another object
.token accesses that object's token property value
E.g.:
var json = '[{"body":{"token":"xxxxxxx"}},{"other":"aaa"}]';
var data = JSON.parse(json)
console.log(data[0].body.token);
Related
I have a response which is of the below format,
{
"access_token": "eWcWLctGW-_NgGVAmFbO9l-nt3yztFzlZCLLlilI9mGDcM5q8d0kQw0uzvFOoXynHcb-MuPVJGTGkSkBhrr69_-aN1r5j9zB4fCl4u4aqOQ-scNI36xgHeGYpXky60drIBpMI83FGqd9pMjL4GWXjFHq61nhJ6xkGj1u1r9a5u6EJrB1lfjNhljzC_j65xaqxtubQ4AglKFO2ib-levpvnd_bEU-QGQrtvS2QbaXhb_hlnX8czo61Gn_OQyBVk7HbN1SozxIPe3RBvf5AiCAouDMz1WMHy9ybVFy8SnoNIgszjo7Ev2IEWS9aFb87u6bvoJvSVJv7s3z-2GUvG2kwfOk2sUWrmq0QeIJJrYwdKQfs3T8HrK2MNKSGteJ04-O",
"token_type": "bearer",
"expires_in": 1799,
"refresh_token": "f1005c7fd74247069dbdb078ee379410",
"as:client_id": "438dc832-33c7-413b-9c71-d0b98a196e6a",
"userName": "master",
".issued": "Fri, 20 Jan 2017 14:30:09 GMT",
".expires": "Fri, 20 Jan 2017 15:00:09 GMT"
}
I'm not sure how to access .issued , .expires and as:client_id
I'm using angular and passing username, password and company_id and getting the response in the above format.
dataService.getAuthToken($scope.username, $scope.password, $scope.company_password).then(function (response) {
//response data here
});
I can easily get token_type, access_token by just using response.data.access_token but not sure how to access .issued , .expires and as:client_id
You can access every property of an object in JavaScript by the indexer syntax, like if it was a map (because an object is a map in javascript):
var issued = response.data[".issued"];
var expires = response.data[".expires"];
var asClient_id= response.data["as:client_id"];
See this link: http://www.w3schools.com/js/js_objects.asp
Accessing Object Properties
You can access object properties in two ways:
objectName.propertyName
or
objectName["propertyName"]
Am trying to access the data from the following JSON . Am Getting response after hitting the server and i need to access the data present in the information1 field.
{
"Test1":[
{
"id1":0,
"Test2":[
{
"Information1":"info1",
"name":"Testing",
"defnitions":[
{
"displayname":"displayame"
},
{
"displayname2":"displayame2"
}
],
"information2":"info2"
}
],
"information3":"info3",
"information4":"info4"
}
]
}
Cause you are using square brackets, it means you have an array. Consequently, to access to your neccessary object you should use:
var data={
"Test1":[
{
"id1":0,
"Test2":[
{
"Information1":"info1",
"name":"Testing",
"defnitions":[
{
"displayname":"displayame"
},
{
"displayname2":"displayame2"
}
],
"information2":"info2"
}
],
"information3":"info3",
"information4":"info4"
}
]
};
var yourObject=data.Test1[0].Test2[0];
To parse JSON response just use stringify method:
var theWholeObject=JSON.stringify(data);
I would like to parse the following String:
{
date: [ 'Thu, 28 Apr 2016 10:56:13 +0200' ],
subject: [ 'Subject' ],
from: [ 'Blob <blob#test.com>' ],
to: [ '<blab#test.com>' ]
}
In order to access the variable date, subject etc ...
But I am not sure how to do it since it
Is not a valid JSON
It is not a structure I know
And I don't want to re-invent the wheel if a solution exist which I am not (yet) aware of.
Any ideas?
EDIT
Data are getting using a node-imap module (only relevant part)
f.on('message', function(msg, seqno) {
console.log('Message #%d', seqno);
var prefix = '(#' + seqno + ') ';
msg.on('body', function(stream, info) {
var buffer = '';
stream.on('data', function(chunk) {
buffer += chunk.toString('utf8');
});
stream.once('end', function() {
var parsedHeader = inspect(Imap.parseHeader(buffer));
console.log('Author: '+parsedHeader);
});
SOLVED
See the comment of #stdob--. Imap.parseHeader() return an object.
Looks like that Imap.parseHeader already returns an object with keys
Try console.log( Object.keys(parsedHeader) to see all the keys.
I have an extra question based on the one I asked before:
calculate frequency using mongodb aggregate framework
so my data in MongoDB looks like this now:
{
"data": {
"interaction": {
"created_at": "Wed, 09 Apr 2014 14:38:16 +0000"
}
},
"_id": {
"$oid": "53455b59edcd5e4e3fdd4ebb"
}
}
before I used to have it like:
[
{
created_at: "2014-03-31T22:30:48.000Z",
id: 450762158586880000,
_id: "5339ec9808eb125965f2eae1"
}
]
so to access created_at I was using mapper like:
var mapper = function () {
if ( this.created_at.getTime() > ( last_date + 10000 ) ) {
...
but as the structure in my database has changed, I tried to change:
this.created_at.getTime()
to:
this.data.interaction.created_at.getTime()
but unfortunately it didn't work out. Thank you for any help
Hate to make this that simple but all you want to do when importing these date strings is this:
new Date("Wed, 09 Apr 2014 14:38:16 +0000")
Which will return a proper date type that you actually should be inserting as part of your data.
I have a script that uses Node.js to request headers from a specific site.
var http = require("http");
var fs = require("fs");
var hostNames = ['www.google.com'];
var options = {
host: hostNames[i],
path: '/'
};
http.get(options, function(res) {
var obj = {};
obj.statusCode = res.statusCode;
obj.headers = res.headers;
console.log(JSON.stringify(obj, null, 4));
})
The output, for the URL "www.google.com" would be attached below:
{
"statusCode": 200,
"headers": {
"date": "Mon, 04 Mar 2013 16:43:39 GMT",
"expires": "-1",
"cache-control": "private, max-age=0",
"content-type": "text/html; charset=ISO-8859-1",
"set-cookie": [
"PREF=ID=cfa31a2cae817ca6:FF=0:TM=1362415419:LM=1362415419:S=m-sNTevwPhFFWVpv; expires=Wed, 04-Mar-2015 16:43:39 GMT; path=/; domain=.google.com",
"NID=67=AKMqJ9Q94GtcmF0kTOAOLgFLqz9XAnSwVe4jzzXFVhvxuxRJP_l9QEwbjR3F7d506thF9BURyGJUz5DuNTEzXesit50Dm7FlOoVuL2qGRt9XZwRMGjAlxL5heO4vIATp; expires=Tue, 03-Sep-2013 16:43:39 GMT; path=/; domain=.google.com; HttpOnly"
],
"p3p": "CP=\"This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info.\"",
"server": "gws",
"x-xss-protection": "1; mode=block",
"x-frame-options": "SAMEORIGIN",
"transfer-encoding": "chunked"
}
}
My question is in-regards to JSON. I am trying to store the output into MongoDB. MongoDB stores JSON-like documents. From my understanding, SQL-based databases have a primary key. This is where my confusion comes in. I would like to use the URL, in this case, 'www.google.com' as the primary key. How do I achieve this? This is my first time using JSON-like storing structures, and the multiple articles I have read do not really apply to my specific situation.
When I search for "www.google.com" in the database, the plan is to have the headers show up, under "www.google.com." I don't know - I think I am still thinking in the SQL mindset. Can someone share some insight to this?
Here is official docs on object ids.
So you can create your own object id for a record using anything with appropriate format (hex number) and length, so this will work
db.names.insert({"_id": new ObjectId("012345678901234567890123"), "name" : "my name" })
but this dont
db.names.insert({"_id": new ObjectId("my reallllly long string"), "name" : "my name" })
you will need to use hash of your url if you want to using at object id.
However mongo gives you another option. leave _id field alone and create url field for url, and than set index on url field
db.scrapedPages.ensureIndex({ 'url': 1})
UPDATE: more specifically to your example.
You are not going to set/change _id property, mongo does it for you.
Instead you are going set url property of document to save, and reasonable thing to use here is your options object, as it defines the page you are parsing.
So I think you'll endup with something like that ( I expect you use mongo native driver and have mongo connection open )
var options = {
host: hostNames[i],
path: '/'
};
http.get(options, function(res) {
var obj = {
url: options.host + options.path // or whatever else is
statusCode : res.statusCode,
headers : res.headers
}
save(obj, function(err, objects) {
if (err) console.warn(err.message);
})
})
function save(doc, callback) {
var collection = new mongodb.Collection(client, 'test_collection')
, cb = callback || function() {}
collection.insert(doc, {safe:true}, cb);
}
The primary key in an SQL table is a column that is used to uniquely identify a particular row. In mongodb _id is the field which is the primary key. mongodb adds it automatically if you don't specify it and assigns an ObjectId (12 byte long BSON identifier) to it. You can check the details here.