Related
I've taken the following JSON example directly from Twitter's API example using Postman, my question is how would I be able to grab each tweets inside the "data" section and add each individual tweet onto a List < String > so that each individual tweet's sub JSON is saved as String in the List. Would this be possible? I was attempting to use the JSON parsing method decoding and encoding from Dart but that did not work, I'm not sure if its because the JSON example features the "data" section and the "errors" section. Any help would be greatly appreciated, thank you!
Example JSON:
{
"data": [
{
"author_id": "12",
"conversation_id": "20",
"created_at": "2006-03-21T20:50:14.000Z",
"id": "20",
"text": "just setting up my twttr"
},
{
"attachments": {
"media_keys": [
"16_1276500934466703361"
]
},
"author_id": "783214",
"conversation_id": "1275244210439028736",
"created_at": "2020-06-23T01:48:07.000Z",
"entities": {
"urls": [
{
"start": 112,
"end": 135,
"url": "https://twitter.com/Twitter/status/1275244210439028736/photo/1",
"expanded_url": "https://twitter.com/Twitter/status/1275244210439028736/photo/1",
"display_url": "pic.twitter.com/dpI2lRmj9F"
}
]
},
"id": "1275244210439028736",
"text": "Need to follow what’s happening in real time? Change your timeline to show latest Tweets instead of top Tweets."
},
{
"attachments": {
"media_keys": [
"3_1274087263073255425"
]
},
"author_id": "783214",
"conversation_id": "1274087687469715457",
"created_at": "2020-06-19T21:12:32.000Z",
"entities": {
"mentions": [
{
"start": 13,
"end": 22,
"username": "YoliZama"
}
],
"urls": [
{
"start": 23,
"end": 46,
"url": "https://twitter.com/Twitter/status/1274087695145332736/photo/1",
"expanded_url": "https://twitter.com/Twitter/status/1274087695145332736/photo/1",
"display_url": "pic.twitter.com/lcGDLzAJIn"
}
]
},
"id": "1274087695145332736",
"referenced_tweets": [
{
"type": "replied_to",
"id": "1274087694105075714"
}
],
"text": "📍 Oakland\n🗣️ #YoliZama"
},
{
"attachments": {
"media_keys": [
"3_1274086977952833536"
]
},
"author_id": "783214",
"conversation_id": "1274087687469715457",
"created_at": "2020-06-19T21:12:32.000Z",
"entities": {
"mentions": [
{
"start": 19,
"end": 31,
"username": "Afrikkana95"
}
],
"urls": [
{
"start": 32,
"end": 55,
"url": "https://twitter.com/Twitter/status/1274087694105075714/photo/1",
"expanded_url": "https://twitter.com/Twitter/status/1274087694105075714/photo/1",
"display_url": "pic.twitter.com/tEfs27p7xu"
}
]
},
"id": "1274087694105075714",
"referenced_tweets": [
{
"type": "replied_to",
"id": "1274087692003770368"
}
],
"text": "📍 New York City\n🗣️ #Afrikkana95"
},
{
"attachments": {
"media_keys": [
"3_1274086862907305984"
]
},
"author_id": "783214",
"conversation_id": "1274087687469715457",
"created_at": "2020-06-19T21:12:31.000Z",
"entities": {
"mentions": [
{
"start": 13,
"end": 25,
"username": "JoshuaKissi"
}
],
"urls": [
{
"start": 26,
"end": 49,
"url": "https://twitter.com/Twitter/status/1274087692003770368/photo/1",
"expanded_url": "https://twitter.com/Twitter/status/1274087692003770368/photo/1",
"display_url": "pic.twitter.com/ZeD3XvJUbX"
}
]
},
"id": "1274087692003770368",
"referenced_tweets": [
{
"type": "replied_to",
"id": "1274087690758090752"
}
],
"text": "📍 Chicago\n🗣️ #JoshuaKissi"
},
{
"attachments": {
"media_keys": [
"3_1274086703272038401"
]
},
"author_id": "783214",
"conversation_id": "1274087687469715457",
"created_at": "2020-06-19T21:12:31.000Z",
"entities": {
"mentions": [
{
"start": 18,
"end": 33,
"username": "Imani_Barbarin"
}
],
"urls": [
{
"start": 34,
"end": 57,
"url": "https://twitter.com/Twitter/status/1274087690758090752/photo/1",
"display_url": "pic.twitter.com/ZRDUipsu38",
"expanded_url": "https://twitter.com/Twitter/status/1274087690758090752/photo/1",
"display_url": "pic.twitter.com/ZRDUipsu38"
}
]
},
"id": "1274087690758090752",
"referenced_tweets": [
{
"type": "replied_to",
"id": "1274087689487134720"
}
],
"text": "📍 Philadelphia\n🗣️ #Imani_Barbarin "
},
{
"attachments": {
"media_keys": [
"3_1274086530919718917"
]
},
"author_id": "783214",
"conversation_id": "1274087687469715457",
"created_at": "2020-06-19T21:12:30.000Z",
"entities": {
"mentions": [
{
"start": 13,
"end": 25,
"username": "BerniceKing"
}
],
"urls": [
{
"start": 26,
"end": 49,
"url": "https://twitter.com/Twitter/status/1274087688321200128/photo/1",
"expanded_url": "https://twitter.com/Twitter/status/1274087688321200128/photo/1",
"display_url": "pic.twitter.com/83upyVnwIS"
}
]
},
"id": "1274087688321200128",
"referenced_tweets": [
{
"type": "replied_to",
"id": "1274087687469715457"
}
],
"text": "📍 Atlanta\n🗣️ #BerniceKing "
},
{
"attachments": {
"media_keys": [
"3_1274086027544498176"
]
},
"author_id": "783214",
"conversation_id": "1274087687469715457",
"created_at": "2020-06-19T21:12:30.000Z",
"entities": {
"mentions": [
{
"start": 17,
"end": 29,
"username": "FredTJoseph"
}
],
"urls": [
{
"start": 30,
"end": 53,
"url": "https://twitter.com/Twitter/status/1274087687469715457/photo/1",
"expanded_url": "https://twitter.com/Twitter/status/1274087687469715457/photo/1",
"display_url": "pic.twitter.com/lNTOkyguG1"
}
]
},
"id": "1274087687469715457",
"text": "📍 Minneapolis\n🗣️ #FredTJoseph"
},
{
"author_id": "783214",
"conversation_id": "1274034244700930049",
"created_at": "2020-06-19T17:40:09.000Z",
"entities": {
"hashtags": [
{
"start": 106,
"end": 115,
"tag": "BlackJoy"
}
],
"mentions": [
{
"start": 3,
"end": 14,
"username": "Blackbirds"
}
]
},
"id": "1274034244700930049",
"referenced_tweets": [
{
"type": "retweeted",
"id": "1274014870707437570"
}
],
"text": "RT #Blackbirds: Juneteenth is a celebration. It’s about our freedom. And within that freedom is our joy.\n\n#BlackJoy is a form of resistance…"
},
{
"author_id": "773578328498372608",
"conversation_id": "1275473478779469825",
"created_at": "2020-06-23T16:59:09.000Z",
"entities": {
"mentions": [
{
"start": 3,
"end": 10,
"username": "Policy"
}
]
},
"id": "1275473478779469825",
"referenced_tweets": [
{
"type": "retweeted",
"id": "1275192966953476100"
}
],
"text": "RT #Policy: Statement on US high-skilled immigration proclamation: \n\n\"This proclamation undermines America’s greatest economic asset: its d…"
}
],
"errors": [
{
"detail": "Could not find tweet with ids: [1276230436478386177].",
"title": "Not Found Error",
"resource_type": "tweet",
"parameter": "ids",
"value": "1276230436478386177",
"type": "https://api.twitter.com/2/problems/resource-not-found"
}
]
}
Output: List < String > where one element in the list could look like this:
{
"attachments": {
"media_keys": [
"16_1276500934466703361"
]
},
"author_id": "783214",
"conversation_id": "1275244210439028736",
"created_at": "2020-06-23T01:48:07.000Z",
"entities": {
"urls": [
{
"start": 112,
"end": 135,
"url": "https://twitter.com/Twitter/status/1275244210439028736/photo/1",
"expanded_url": "https://twitter.com/Twitter/status/1275244210439028736/photo/1",
"display_url": "pic.twitter.com/dpI2lRmj9F"
}
]
},
"id": "1275244210439028736",
"text": "Need to follow what’s happening in real time? Change your timeline to show latest Tweets instead of top Tweets."
},
It would essentially grab one whole tweet's subJson and save it onto the List. This goes hand in hand with an issue I was facing a few days back concerning this problem , but I think the problem is that the JSON response I get back using the HTTP library is not able to be added properly onto a List.
List<Map<String, dynamic>> jsonRes =
(inputMap['data'] as List<Map>).map((t) => {
if (t.containsKey('attachments')) 'attachments': t['attachments'],
if (t.containsKey('author_id')) 'author_id': t['author_id'],
if (t.containsKey('conversation_id')) 'conversation_id': t['conversation_id'],
if (t.containsKey('created_at')) 'created_at': t['created_at'],
if (t.containsKey('entities')) 'entities': t['entities'],
if (t.containsKey('id')) 'id': t['id'],
if (t.containsKey('text')) 'text': t['text'],
}).toList();
List<String> stringRes = jsonRes.map((e) => jsonEncode(e)).toList();
Alternatively:
final List<String> wantedKeys = [
'attachments',
'author_id',
'conversation_id',
'created_at',
'entities',
'id',
'text'
];
List<Map<String, dynamic>> jsonRes = (inputMap['data']
as List<Map<String, dynamic>>)
.map((m) => Map.fromEntries(m.entries.where((e) => wantedKeys.contains(e.key))))
.toList();
List<String> stringRes = jsonRes.map((e) => jsonEncode(e)).toList();
In a log I’d like to match all json objects which type is "sync.out.notify.job.status" and print them. I’m on a Mac (zsh).
I tried: grep -Eo \{.+"sync\.out\.notify\.job\.status".+\}
Mon Jan 11 2021 13:08:46 GMT+0100 (Central European Standard Time) CoreSyncNotifications: sync.out.notify.job.status:
{
"cloudid": "00000000-0000-0000-0000-000000000000",
"job": "files",
"majorstate": "syncing",
"minorstate": "transferring",
"requestid": "45768456-bd58-4cb5-9dff-dfgjfgdj456",
"type": "sync.out.notify.job.status",
"userid": "XXXXXXXXXXXXXXXXXXXXXXXX#AdobeID",
"version": "1.1.43711"
}
Mon Jan 11 2021 13:08:46 GMT+0100 (Central European Standard Time) Vulcan: <- COSY 4.3: sync.out.notify.job.status
Mon Jan 11 2021 13:08:46 GMT+0100 (Central European Standard Time) CoreSyncNotifications: sync.out.notify.job.status:
{
"errors": "",
"cloudid": "00000000-0000-0000-0000-000000000000",
"job": "files",
"majorstate": "idle",
"minorstate": "complete",
"requestid": "4856658fdgh-efb3-4da9-b5f5-4856658fgj",
"type": "sync.out.notify.job.status",
"userid": "XXXXXXXXXXXXXXXXXXXXXXXX#AdobeID",
"version": "1.1.43711"
}
Mon Jan 11 2021 13:08:46 GMT+0100 (Central European Standard Time) WebSocketServer: client_7: SEND syncstate creative_cloud: ok
grep usually searches pattern in single line only unless you use -z option in gnu grep.
You may try this gnu awk command:
awk -v RS='(^|\n){[^}]+?sync.out.notify.job.status[^}]+}(\n|$)' 'RT {printf "%s", RT}' file
{
"cloudid": "00000000-0000-0000-0000-000000000000",
"job": "files",
"majorstate": "syncing",
"minorstate": "transferring",
"requestid": "45768456-bd58-4cb5-9dff-dfgjfgdj456",
"type": "sync.out.notify.job.status",
"userid": "XXXXXXXXXXXXXXXXXXXXXXXX#AdobeID",
"version": "1.1.43711"
}
{
"errors": "",
"cloudid": "00000000-0000-0000-0000-000000000000",
"job": "files",
"majorstate": "idle",
"minorstate": "complete",
"requestid": "4856658fdgh-efb3-4da9-b5f5-4856658fgj",
"type": "sync.out.notify.job.status",
"userid": "XXXXXXXXXXXXXXXXXXXXXXXX#AdobeID",
"version": "1.1.43711"
}
You can easily and robustly use a JSON-aware tool like jq for this, assuming you want to print the matching blocks:
$ grep -v '^[[:alpha:]]' file | jq 'select(.type=="sync.out.notify.job.status")'
{
"cloudid": "00000000-0000-0000-0000-000000000000",
"job": "files",
"majorstate": "syncing",
"minorstate": "transferring",
"requestid": "45768456-bd58-4cb5-9dff-dfgjfgdj456",
"type": "sync.out.notify.job.status",
"userid": "XXXXXXXXXXXXXXXXXXXXXXXX#AdobeID",
"version": "1.1.43711"
}
{
"errors": "",
"cloudid": "00000000-0000-0000-0000-000000000000",
"job": "files",
"majorstate": "idle",
"minorstate": "complete",
"requestid": "4856658fdgh-efb3-4da9-b5f5-4856658fgj",
"type": "sync.out.notify.job.status",
"userid": "XXXXXXXXXXXXXXXXXXXXXXXX#AdobeID",
"version": "1.1.43711"
}
Here's another way to get from { to } to pipe to jq:
$ sed -n '/^{/,/^}/p' file
{
"cloudid": "00000000-0000-0000-0000-000000000000",
"job": "files",
"majorstate": "syncing",
"minorstate": "transferring",
"requestid": "45768456-bd58-4cb5-9dff-dfgjfgdj456",
"type": "sync.out.notify.job.status",
"userid": "XXXXXXXXXXXXXXXXXXXXXXXX#AdobeID",
"version": "1.1.43711"
}
{
"errors": "",
"cloudid": "00000000-0000-0000-0000-000000000000",
"job": "files",
"majorstate": "idle",
"minorstate": "complete",
"requestid": "4856658fdgh-efb3-4da9-b5f5-4856658fgj",
"type": "sync.out.notify.job.status",
"userid": "XXXXXXXXXXXXXXXXXXXXXXXX#AdobeID",
"version": "1.1.43711"
}
I normally wouldn't suggest using a range expression over using a flag but in this case since it's ALL you're going to do with it, it's probably fine.
https://bern.korea.ac.kr/pubmed/32818866
$ jq -r '.[] | .denotations | .[] | select(.obj=="drug") | .span | [.begin, .end] | #tsv'
I am able to extract the following info from the above URL using the above jq command.
377 387
562 579
584 602
659 676
681 699
919 936
941 959
1032 1049
1054 1072
But the output that I really need is the following.
The last column is just the substring of text starting from begin+1 to end (suppose the string in text is indexed starting from 1.
I don't know how to extract this info using just jq as it involves taking a parent sibling element and the substring of another parent sibling element. Could anybody show me how to extract the output in this format? Thanks.
32818866 377 387 silica gel
32818866 562 579 7-methoxycoumarin
32818866 584 602 8-prenylkaempferol
32818866 659 676 7-methoxycoumarin
32818866 681 699 8-prenylkaempferol
32818866 919 936 7-methoxycoumarin
32818866 941 959 8-prenylkaempferol
32818866 1032 1049 7-methoxycoumarin
32818866 1054 1072 8-prenylkaempferol
The json txt is here for the completeness of this message.
[
{
"project": "BERN",
"sourcedb": "PubMed",
"sourceid": "32818866",
"text": "Identification of two bitter components in Zanthoxylum bungeanum Maxim. and exploration of their bitter taste mechanism through receptor hTAS2R14. Bitterness is an inherent organoleptic characteristic affecting the flavor of Zanthoxylum bungeanum Maxim. In this study, the vital bitter components of Z. bungeanum were concentrated through solvent extraction, sensory analysis, silica gel chromatography, and thin-layer chromatographic techniques and subsequently identified by UPLC-Q-TOF-MS. Two components with the highest bitterness intensities (BIs), such as 7-methoxycoumarin and 8-prenylkaempferol were selected. The bitter taste perceived thresholds of 7-methoxycoumarin and 8-prenylkaempferol were 0.062 mmol/L and 0.022 mmol/L, respectively. Moreover, the correlation between the contents of the two bitter components and the BIs of Z. bungeanum were proved. The results of siRNA and flow cytometry showed that 7-methoxycoumarin and 8-prenylkaempferol could activate the bitter receptor hTAS2R14. The results concluded that 7-methoxycoumarin and 8-prenylkaempferol contribute to the bitter taste of Z. bungeanum.",
"denotations": [
{
"id": [
"NCBI:txid328401"
],
"span": {
"begin": 43,
"end": 64
},
"obj": "species"
},
{
"id": [
"CUI-less"
],
"span": {
"begin": 128,
"end": 145
},
"obj": "gene"
},
{
"id": [
"NCBI:txid328401"
],
"span": {
"begin": 225,
"end": 246
},
"obj": "species"
},
{
"id": [
"NCBI:txid328401"
],
"span": {
"begin": 300,
"end": 312
},
"obj": "species"
},
{
"id": [
"MESH:D058428",
"BERN:315272203"
],
"span": {
"begin": 377,
"end": 387
},
"obj": "drug"
},
{
"id": [
"CHEBI:5679",
"BERN:4597103"
],
"span": {
"begin": 562,
"end": 579
},
"obj": "drug"
},
{
"id": [
"MESH:C532177",
"BERN:280529003"
],
"span": {
"begin": 584,
"end": 602
},
"obj": "drug"
},
{
"id": [
"CHEBI:5679",
"BERN:4597103"
],
"span": {
"begin": 659,
"end": 676
},
"obj": "drug"
},
{
"id": [
"MESH:C532177",
"BERN:280529003"
],
"span": {
"begin": 681,
"end": 699
},
"obj": "drug"
},
{
"id": [
"NCBI:txid328401"
],
"span": {
"begin": 841,
"end": 853
},
"obj": "species"
},
{
"id": [
"CHEBI:5679",
"BERN:4597103"
],
"span": {
"begin": 919,
"end": 936
},
"obj": "drug"
},
{
"id": [
"MESH:C532177",
"BERN:280529003"
],
"span": {
"begin": 941,
"end": 959
},
"obj": "drug"
},
{
"id": [
"CUI-less"
],
"span": {
"begin": 979,
"end": 994
},
"obj": "gene"
},
{
"id": [
"CUI-less"
],
"span": {
"begin": 995,
"end": 1003
},
"obj": "gene"
},
{
"id": [
"CHEBI:5679",
"BERN:4597103"
],
"span": {
"begin": 1032,
"end": 1049
},
"obj": "drug"
},
{
"id": [
"MESH:C532177",
"BERN:280529003"
],
"span": {
"begin": 1054,
"end": 1072
},
"obj": "drug"
},
{
"id": [
"NCBI:txid328401"
],
"span": {
"begin": 1107,
"end": 1119
},
"obj": "species"
}
],
"timestamp": "Wed Oct 28 21:43:04 +0000 2020",
"logits": {
"disease": [],
"gene": [
[
{
"start": 128,
"end": 145,
"id": "CUI-less"
},
0.7066106796264648
],
[
{
"start": 979,
"end": 994,
"id": "CUI-less"
},
0.9999749660491943
],
[
{
"start": 995,
"end": 1003,
"id": "CUI-less"
},
0.9052715301513672
]
],
"drug": [
[
{
"start": 377,
"end": 387,
"id": "MESH:D058428\tBERN:315272203"
},
0.999982476234436
],
[
{
"start": 562,
"end": 579,
"id": "CHEBI:5679\tBERN:4597103"
},
0.9999980926513672
],
[
{
"start": 584,
"end": 602,
"id": "MESH:C532177\tBERN:280529003"
},
0.9999980926513672
],
[
{
"start": 659,
"end": 676,
"id": "CHEBI:5679\tBERN:4597103"
},
0.9999980926513672
],
[
{
"start": 681,
"end": 699,
"id": "MESH:C532177\tBERN:280529003"
},
0.9999980330467224
],
[
{
"start": 919,
"end": 936,
"id": "CHEBI:5679\tBERN:4597103"
},
0.9999980926513672
],
[
{
"start": 941,
"end": 959,
"id": "MESH:C532177\tBERN:280529003"
},
0.9999980926513672
],
[
{
"start": 1032,
"end": 1049,
"id": "CHEBI:5679\tBERN:4597103"
},
0.9999980926513672
],
[
{
"start": 1054,
"end": 1072,
"id": "MESH:C532177\tBERN:280529003"
},
0.9999980926513672
]
],
"species": [
[
{
"start": 43,
"end": 64,
"id": "NCBI:txid328401"
},
0.9999997615814209
],
[
{
"start": 225,
"end": 246,
"id": "NCBI:txid328401"
},
0.9999998211860657
],
[
{
"start": 300,
"end": 312,
"id": "NCBI:txid328401"
},
0.9999998211860657
],
[
{
"start": 841,
"end": 853,
"id": "NCBI:txid328401"
},
0.9999998211860657
],
[
{
"start": 1107,
"end": 1119,
"id": "NCBI:txid328401"
},
0.9999998211860657
]
]
},
"elapsed_time": {
"tmtool": 0.991,
"ner": 0.453,
"normalization": 0.172,
"total": 1.617
}
}
]
Assuming the first column of the desired output is the "sourceid", we can adapt your solution as follows:
.[]
| .sourceid as $id
| .text as $text
| .denotations[]
| select(.obj=="drug")
| .span
| [$id, .begin, .end, $text[.begin : .end] ]
| #tsv
{
"result": [
{
"id": "140964",
"label": " 3688-RELT-PRD-LOGIN",
"location": "St. Louis, MO",
"startdate": "September 1, 2019 00:00:00",
"enddate": "September 12, 2019 04:57:41",
"statushistory": [
{
"status": "DOWN",
"statustype": "Content error",
"starttime": "09-01-2019 00:00",
"endtime": "09-11-2019 23:57",
"duration": "950261"
}
]
},
{
"id": "142285",
"label": " 4316_Rebar Messaging_PROD",
"location": "Chicago, IL",
"startdate": "September 1, 2019 00:00:00",
"enddate": "September 12, 2019 04:57:41",
"statushistory": [
{
"status": "UP",
"statustype": "OK",
"starttime": "09-01-2019 00:00",
"endtime": "09-11-2019 23:57",
"duration": "950261"
}
]
},
{
"id": "153272",
"label": "10002-Self Service SQL Failover",
"location": "Miami, FL",
"startdate": "September 1, 2019 00:00:00",
"enddate": "September 12, 2019 04:57:41",
"statushistory": [
{
"status": "UP",
"statustype": "OK",
"starttime": "09-01-2019 00:00",
"endtime": "09-11-2019 23:57",
"duration": "950261"
}
]
},
{
"id": "156764",
"label": "10054-SMARTSUPPLY-PRD",
"location": "Bangalore, India",
"startdate": "September 1, 2019 00:00:00",
"enddate": "September 12, 2019 04:57:41",
"statushistory": [
{
"status": "UP",
"statustype": "OK",
"starttime": "09-01-2019 00:00",
"endtime": "09-11-2019 23:57",
"duration": "950261"
}
]
},
{
"id": "156764",
"label": "10054-SMARTSUPPLY-PRD",
"location": "Mumbai, India",
"startdate": "September 1, 2019 00:00:00",
"enddate": "September 12, 2019 04:57:41",
"statushistory": [
{
"status": "UP",
"statustype": "OK",
"starttime": "09-08-2019 10:35",
"endtime": "09-11-2019 23:57",
"duration": "307316"
},
{
"status": "DOWN",
"statustype": "Timeout warning",
"starttime": "09-08-2019 10:31",
"endtime": "09-08-2019 10:35",
"duration": "274"
},
{
"status": "UP",
"statustype": "OK",
"starttime": "09-01-2019 00:00",
"endtime": "09-08-2019 10:31",
"duration": "642670"
}
]
}
]
}
i have this above json file .
which holds multiple data set this is just an example with 5 .
i want to pull the status value from the above json .
We can usual do it by result['statushistory'][0]['status']
But it pulls only the 5 values . It is returning only the first value of
the last statushistory .
How can i write a code which returns all the value of statushistory ?
And also if the statushistory doesnt have any status how can i mange that in the code ?(i.e return status only if statushistory has status present in it)
Something like:
statushistory_list = [result["statushistory"] for result in json_data["result"]]
status_list = [
[item["status"] for item in statushistory if "status" in item]
for statushistory in statushistory_list
]
this will get you:
[['DOWN'], ['UP'], ['UP'], ['UP'], ['UP', 'DOWN', 'UP']]
If it's not what you are looking for then, please show an example of the output you would like.
I have a project I’m working on but I know very little about Javascript/Jquery/Ajax and am having trouble figuring out exactly what I need to do and don’t really have anyone around me to ask. Using the JSON info below, I need to include an Ajax call to populate current 'condition.temp’, the returned 'temperature' unit, and set the weather to be linked to 'link' key in the returned response. I’d like to return it to the console. I have it set up to return all of the JSON data to the console, but am stuck on how to return just the particular data items I need.
{
"success": true,
"data": [
{
"units": {
"distance": "mi",
"speed": "mph",
"pressure": "in",
"temperature": "F"
},
"language": "en-us",
"description": "Yahoo! Weather for Paducah, KY, US",
"astronomy": {
"sunrise": "5:45 am",
"sunset": "7:57 pm"
},
"item": {
"forecast": [
{
"day": "Mon",
"text": "Mostly Cloudy",
"date": "16 May 2016",
"high": 61,
"low": 47,
"code": 28
},
{
"day": "Tue",
"text": "Scattered Showers",
"date": "17 May 2016",
"high": 62,
"low": 51,
"code": 39
},
{
"day": "Wed",
"text": "Scattered Showers",
"date": "18 May 2016",
"high": 66,
"low": 50,
"code": 39
},
{
"day": "Thu",
"text": "Mostly Cloudy",
"date": "19 May 2016",
"high": 71,
"low": 51,
"code": 28
},
{
"day": "Fri",
"text": "Showers",
"date": "20 May 2016",
"high": 73,
"low": 58,
"code": 11
},
{
"day": "Sat",
"text": "Scattered Thunderstorms",
"date": "21 May 2016”,
"high": 74,
"low": 61,
"code": 47
},
{
"day": "Sun",
"text": "Partly Cloudy",
"date": "22 May 2016”,
"high": 76,
"low": 59,
"code": 30
},
{
"day": "Mon",
"text": "Partly Cloudy",
"date": "23 May 2016”,
"high": 81,
"low": 61,
"code": 30
},
{
"day": "Tue",
"text": "Thunderstorms",
"date": "24 May 2016",
"high": 81,
"low": 64,
"code": 4
},
{
"day": "Wed",
"text": "Rain",
"date": "25 May 2016",
"high": 82,
"low": 67,
"code": 12
}
],
"lat": 37.030182,
"guid": {
"isPermaLink": false
},
"condition": {
"text": "Cloudy",
"date": "Mon, 16 May 2016 03:00 PM CDT",
"temp": 60,
"code": 26
},
"description": "<![CDATA[<img src=\"http://l.yimg.com/a/i/us/we/52/26.gif\"/>\n<BR />\n<b>Current Conditions:</b>\n<BR />Cloudy\n<BR />\n<BR />\n<b>Forecast:</b>\n<BR /> Mon - Mostly Cloudy. High: 61Low: 47\n<BR /> Tue - Scattered Showers. High: 62Low: 51\n<BR /> Wed - Scattered Showers. High: 66Low: 50\n<BR /> Thu - Mostly Cloudy. High: 71Low: 51\n<BR /> Fri - Showers. High: 73Low: 58\n<BR />\n<BR />\nFull Forecast at Yahoo! Weather\n<BR />\n<BR />\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)\n<BR />\n]]>",
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-12775789/",
"pubDate": "Mon, 16 May 2016 03:00 PM CDT",
"long": -88.712601,
"title": "Conditions for Paducah, KY, US at 03:00 PM CDT"
},
"image": {
"link": "http://weather.yahoo.com",
"height": 18,
"url": "http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif",
"width": 142,
"title": "Yahoo! Weather"
},
"ttl": 60,
"atmosphere": {
"visibility": 16.1,
"humidity": 60,
"pressure": 1007,
"rising": 0
},
"location": {
"country": "United States",
"city": "Paducah",
"region": " KY"
},
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-12775789/",
"lastBuildDate": "Mon, 16 May 2016 04:35 PM CDT",
"wind": {
"direction": 105,
"chill": 59,
"speed": 7
},
"title": "Yahoo! Weather - Paducah, KY, US"
}
]
}
Assuming that you're asking how to extract data from JSON...
If you use the node console, you can dig into it and see the inner workings.
first, assign the json data to a variable, ie
var test = {
"success": true,
"data": [
{
"units": {
"distance": "mi",
"speed": "mph",
"pressure": "in",
"temperature": "F"
},
"language": "en-us", etc....
then start digging down the data using console.log:
> console.log(test);
{ success: true,
data:
[ { units: [Object],
language: 'en-us',
description: 'Yahoo! Weather for Paducah, KY, US',
So, how do we access "success"? This worked:
> console.log(test['success']);
true
But there is a better way:
> console.log(test.success);
true
Now, how do we get the "data" attributes?
> console.log(test.data);
[ { units: { distance: 'mi', speed: 'mph', pressure: 'in', temperature: 'F' },
language: 'en-us',
description: 'Yahoo! Weather for Paducah, KY, US',
astronomy: { sunrise: '5:45 am', sunset: '7:57 pm' },
item:
{ forecast: [Object],
lat: 37.030182,
we could try this:
> console.log(test.data.units);
undefined
Uh, oh... what happened? Short answer, data is an array ( [ )
> console.log(test.data[0].units);
{ distance: 'mi', speed: 'mph', pressure: 'in', temperature: 'F' }
Let's go one more:
> console.log(test.data[0].units.speed);
mph
Using this method you can extract any information out of JSON.