Parse json using javascript with multiple object set - javascript

I have a below output from the server.And I want to get the "result" from the JSON input.
My JSON Input is
{
"header":{
"type":"esummary",
"version":"0.3"
},
"result":{
"28885854":{
"uid":"28885854",
"pubdate":"2017 Sep 8",
"epubdate":"2017 Sep 8",
"source":"J Org Chem",
}
}
}
And I prefer only javascript.Anyone help me to do this.
EDIT :
I tried the code to get JSON from server
var json = JSON.stringify(temp1);
console.log(json)
My original output in the console was like this.
"
{"header":{"type":"esummary","version":"0.3"},"result":{"28885854":{"uid":"28885854","pubdate":"2017 Sep 8","epubdate":"2017 Sep 8","source":"J Org Chem","authors":[{"name":"Farmer LA","authtype":"Author","clusterid":""},{"name":"Haidasz EA","authtype":"Author","clusterid":""},{"name":"Griesser M","authtype":"Author","clusterid":""},{"name":"Pratt DA","authtype":"Author","clusterid":""}],"lastauthor":"Pratt DA","title":"Phenoxazine: A Privileged Scaffold for Radical-Trapping Antioxidants.","sorttitle":"phenoxazine a privileged scaffold for radical trapping antioxidants","volume":"","issue":"","pages":"","lang":["eng"],"nlmuniqueid":"2985193R","issn":"0022-3263","essn":"1520-6904","pubtype":["Journal Article"],"recordstatus":"PubMed - as supplied by publisher","pubstatus":"10","articleids":[{"idtype":"pubmed","idtypen":1,"value":"28885854"},{"idtype":"doi","idtypen":3,"value":"10.1021/acs.joc.7b02025"},{"idtype":"rid","idtypen":8,"value":"28885854"},{"idtype":"eid","idtypen":8,"value":"28885854"}],"history":[{"pubstatus":"entrez","date":"2017/09/09 06:00"},{"pubstatus":"pubmed","date":"2017/09/09 06:00"},{"pubstatus":"medline","date":"2017/09/09 06:00"}],"references":[],"attributes":["Has Abstract"],"pmcrefcount":"","fulljournalname":"The Journal of organic chemistry","elocationid":"doi: 10.1021/acs.joc.7b02025","doctype":"citation","srccontriblist":[],"booktitle":"","medium":"","edition":"","publisherlocation":"","publishername":"","srcdate":"","reportnumber":"","availablefromurl":"","locationlabel":"","doccontriblist":[],"docdate":"","bookname":"","chapter":"","sortpubdate":"2017/09/08 00:00","sortfirstauthor":"Farmer LA","vernaculartitle":""},"uids":["28885854"]}}
"

I think all you're looking for is .result. Perhaps you also want JSON.parse(...) if you have just the JSON string so far.
var obj = {
"header": {
"type": "esummary",
"version": "0.3"
},
"result": {
"28885854": {
"uid": "28885854",
"pubdate": "2017 Sep 8",
"epubdate": "2017 Sep 8",
"source": "J Org Chem",
}
}
};
console.log(obj.result);
// To address edits above and comments below:
console.log(obj.result["28885854"].source);

Related

How would I make this output a smaller more neat string?

I have been messing around with googles APIs a little bit, and I am trying to create a in terminal command that tells me how far away I am from something and how long it would take to get there.
The issue that I am having is that when I run:
var axios = require('axios');
var config = {
method: 'get',
url: 'https://maps.googleapis.com/maps/api/distancematrix/json?origins=Washington%2C%20DC&destinations=New%20York%20City%2C%20NY&units=imperial&key=AIzaSyCbwuhNvOJQrYWnLRF6WjzJeOcnhYYfpZA',
headers: {}
};
axios(config)
.then(function(response) {
console.log(JSON.stringify(response.data.rows));
})
.catch(function(error) {
console.log(error);
});
Right now it is outputting:
[{"elements":[{"distance":{"text":"225 mi","value":361918},"duration":{"text":"3 hours 52 mins","value":13938},"status":"OK"}]}]
And I would LIKE the output format to display it like:
you are 225 miles or 3 hours 52 mins away
How would I go about making the output look like the second supplied example?
You can try this, but I'm assuming you are receiving data in this order
I would suggest you to add null checks as well.
let data = [{
"elements": [{
"distance": {
"text": "225 mi",
"value": 361918
},
"duration": {
"text": "3 hours 52 mins",
"value": 13938
},
"status": "OK"
}]
}];
let firstElement = data[0].elements[0];
let constructedString = `You are ${firstElement.distance.text} or ${firstElement.duration.text} away`;
console.log(constructedString);
response.data.rows is a JSON object. JSON.stringify() converts that into a string, which you don't want - you want the object.
If you don't stringify it, you can access the distance string with response.data.rows[0].elements[0].distance.text, and the time with response.data.rows[0].elements[0].duration.text
You could use the following one-liner:
var data = [{"elements":[{ "distance": {"text": "225 mi", "value": 361918}, "duration": {"text": "3 hours 52 mins", "value": 13938}, "status": "OK"}]}];
console.log(`you are ${ data[0].elements[0].distance.text} or ${data[0].elements[0].duration.text} away`);

how to eliminate all the allocated charectors in javascript like "",'',{} in a string

I have a sentence how do we format that string to support and assign to a variable?
let words="2021-08-03 14:00:49.0430 Info Message Template Auto Format enabled
2021-08-03 14:00:49.4297 Info Adding target ConsoleTarget(Name=consoleLogger)
2021-08-03 14:00:49.8396 Info Found 48 configuration items
2021-08-03 14:00:49.8716 Info Configuration initialized.
2021-08-03 14:00:49.8716 Info NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 4.6.8.10751. Product version: 4.6.8.
{ "time": "2021-08-03 14:00:50.0625", "thread": "1", "level": "WARN", "message": "Storing keys in a directory '\/root\/.aspnet\/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.", "properties": "path=\/root\/.aspnet\/DataProtection-Keys, EventId_Id=60, EventId=60" }
{ "time": "2021-08-03 14:00:50.4593", "thread": "1", "level": "WARN", "message": "No XML encryptor configured. Key {153434f7-eab5-4326-a0aa-5973fb78b`ecb} may be persisted to storage in unencrypted form.", "proper`ties": "KeyId=153434f7-eab5-4326-a0aa-5973fb78becb, EventId_Id=35, EventId=35" }
{ "time": "2021-08-03 14:01:14.0448", "thread": "5", "level": "WARN", "message": "Failed to determine the https port for redirect.", "properties": "EventId_Id=3, EventId_Name=FailedToDeterminePort, EventId=FailedToDeterminePort" }"
console.log(words)
When I tried to do some string methods to in it its not allowing me to perform any action because of the special characters in the sentence.
How can we support this type of sentence to perform string operations?
Expected output: I should be able to get the log.
You may need to use backtick (`) at the start and the end of string and add the escaping sequence \ before any backtick in the middle of the text. Here is the snippet:
let words = `"2021-08-03 14:00:49.0430 Info Message Template Auto Format enabled
2021 - 08 - 03 14: 00: 49.4297 Info Adding target ConsoleTarget(Name = consoleLogger)
2021 - 08 - 03 14: 00: 49.8396 Info Found 48 configuration items
2021 - 08 - 03 14: 00: 49.8716 Info Configuration initialized.
2021 - 08 - 03 14: 00: 49.8716 Info NLog, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = 5120e14c03d0593c.File version: 4.6.8.10751.Product version: 4.6.8.
{ "time": "2021-08-03 14:00:50.0625", "thread": "1", "level": "WARN", "message": "Storing keys in a directory '\/root\/.aspnet\/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.", "properties": "path=\/root\/.aspnet\/DataProtection-Keys, EventId_Id=60, EventId=60" }
{ "time": "2021-08-03 14:00:50.4593", "thread": "1", "level": "WARN", "message": "No XML encryptor configured. Key {153434f7-eab5-4326-a0aa-5973fb78b\`ecb} may be persisted to storage in unencrypted form.", "proper\`ties": "KeyId=153434f7-eab5-4326-a0aa-5973fb78becb, EventId_Id=35, EventId=35" }
{ "time": "2021-08-03 14:01:14.0448", "thread": "5", "level": "WARN", "message": "Failed to determine the https port for redirect.", "properties": "EventId_Id=3, EventId_Name=FailedToDeterminePort, EventId=FailedToDeterminePort" } "
`
console.log(words)

CouchDB view curl returns javascript db

I have some documents in a database sitting in a couchdb server. I'm struggling to create a view that works. The documents are formatted as such:
{
"_id": "2018_1_17",
"_rev": "2-d6b8e691925e9bae697c2df054eb77f0",
"name1.com": {
"Title": "string",
"Date": "Thu, 11 Jan 2018 08:55:21 +0000",
"Site": "string",
"Description": "string"
},
"name2.com": {
"Title": "title",
"Date": "Thu, 11 Jan 2018 06:20:43 +0000",
"Site": "string",
"Description": "string"
},
...
I've tried a couple of views, for example:
function (doc) {
if(doc.Title && doc.Date) {
emit(doc.Title, doc.Date);
}
}
etc.
When I do a curl though, it returns the javascript code of the view and no actual results.
I would guess my map function is just wrong. Any ideas?
Thanks!
You didn't mention your curl command and I don't know why it is not working, but there is something else which doesn't look right:
In your view function your using doc.Title and doc.Date, however, in your document, there is not a doc.Title field and there is not a doc.Date field, since Title and Date are sub-fields inside name1.com and name2.com.
Sorry for not getting back sooner. It seems my curl command wasn't quite right.
It was:
curl http://127.0.0.1:5984/rss_feeds/_design/_by_date/
It needs to be:
curl http://127.0.0.1:5984/rss_feeds/_design/_by_date/_view/_by_date

How can I parse aws api date string?

When I use the aws codecommit api to select one commit I recieve the following json response:
{
"commit": {
"additionalData": "",
"committer": {
"date": "1505892072 +0200",
"name": "some name",
"email": "some#email.com"
},
"author": {
"date": "1505892072 +0200",
"name": "some name",
"email": "some#email.com"
},
"treeId": "c06c3kr2890sdf80f4e7f1234998cc18c2d672a6",
"parents": [
"8jghe808f7f5acc8f067dfg73f88ebfc6e5dfg82"
],
"message": "some message"
}
}
Now I want to parse the commit date 1505892072 +0200 in javascript. For this the function Date.parse(commtiDate) doesn't work because of the confusing format of the date.
In the example response of the AWS documentation it seems as if the format below is the standard format for api response (code commit api reference).
Have someone an idea how this format works and how to parse it in javascript?
I'm not sure about the +0200 but check the following:
var seconds = "1505892072";
var d = new Date(0);
d.setUTCSeconds(seconds);
Possible timezone adjustments still to be made ;)

How do I access values in a d3 json file with nested array objects

Previously I was using a json file with the following format:
[{"lat":43.788458853157117,"lng":-79.282781549043008,"category":"volunteer","name":"Rita","url":"", "description":"xxx is a member of 13"},{"lat":43.7,"lng":-79.4,"category":"organization","name":"TCAN","url":"http://tcan.ca","description":"Lorem ipsum"}]
Now I am attempting to generate the json file from a Drupal site and am getting the following structure. How can I reference the lowest level fields. I have looked at examples using d3.net but have not found any that apply.
{
"organizations": [
{
"member": {
"field_category": "organization",
"body": "A network of organizations in Toronto devoted to climate change mitigation and adaptation.",
"URL": "xxx.ca",
"title": "Toronto Climate Action Network",
"field_lat": 43.7,
"field_long": -79.4
}
},
{
"member": {
"field_category": "abc",
"body": "xxx.",
"URL": "",
"title": "yyy",
"field_lat": 43.7,
"field_long": -79.28
}
}
]
}
Assuming that your data is stored in the variable data:
var bottom = data.organizations.map(function(d) { return d.member; });

Categories

Resources