I've looked around for this and I know the information is out there, but I'm completely ignorant on how to do this. I need to extract just the names from the json data file on Facebook's Graph API. Here's an example of the json data.
{
"id": "POSTID",
"created_time": "2013-09-20T20:20:52+0000",
"comments": {
"data": [
{
"from": {
"name": "XXXXXXX",
"id": "XXXXXXX"
},
"id": "XXXXXXX"
},
{
"from": {
"name": "XXXXXXX",
"id": "XXXXXXX"
},
"id": "XXXXXXX"
}
I need to get just the names in a spreadsheet. How can I achieve this? Thanks a lot for your time.
OK, based on the comment discussion above I knocked up an example to pull the names from a Facebook Graph API - Comment Stream, using jQuery and JSONSelect as a JSON interrogator.
jsFiddle: http://jsfiddle.net/9nqu6/1/
Once the feed is retrieved, all the work is done by JSONSelect, using a selector '.comments .data .from .name' to pick down to the level required in the feed.
The .forEach() command allows iteration on results with a callback, here just generating a table and a CSV file (using a Data URI, file name set in Chrome via the download attribute).
NB. There's no error handling on this, so be sure to pass it the correct type of URI! eg.
https://graph.facebook.com/<postid>?fields=comments.limit(1000).fields(from)
jQuery
$('#read-graph').on('click', function() {
var graphLink = $('#graph-link').val();
if (!graphLink) {
alert("Enter link");
return false;
}
graphLink = graphLink + (/\?/.test(graphLink) ? "&" : "?") + "callback=?"
$.getJSON(graphLink, function(data) {
var nameBlock = $('#name-block');
nameBlock.find('tr').remove();
var csvData = "data:application/csv;charset=utf-8,Index%2CName%2C%0A";
var cIndex = 0;
JSONSelect.forEach('.comments .data .from .name', data, function(cName) {
cIndex++;
nameBlock.append('<tr><td class="index">' + cIndex + '</td><td class="name">' + cName + '</td></tr>');
csvData = csvData + cIndex + "%2C" + encodeURIComponent('"' + cName.replace(/"/g, '""') + '"') + "%0A";
});
$('#download-csv').prop('href', csvData).attr('download', "FBGraph.csv").show();
});
return false;
});
HTML
<h3>Graph Link</h3>
<form>
<input id="graph-link" name="graph-link" type="text" value="" />
<input id="read-graph" name="read-graph" type="submit" value="Read Graph" />
<a id="download-csv" href="#" style="display: none;">Download CSV</a>
</form>
<table id="name-block">
</table>
CSS
#graph-link {
width: 400px;
}
#name-block {
margin-top: 10px;
border: 1px solid black;
border-collapse: collapse;
}
#name-block tr {
border-top: 1px dashed black;
}
#name-block .index {
width: 50px;
}
#name-block .name {
width: 350px;
border-left: 1px solid black;
}
I've written an Excel Add-In (XLL) that pulls JSON from the Facebook Graph API straight into Excel. It's available on GitHub:
https://github.com/spreadgit/XLfacebook
Related
How can I save a Cytoscape network as a flat JSON file?
The cytoscape.js examples at cytoscape.org typically store the underlying network data as a flat data.json file (e.g. colas-graph, see also https://js.cytoscape.org/#notation/elements-json).
This is in contrast to the grouped or keyed format obtained when saving a graph as a Cytoscape JSON file (in Cytoscape: file > Export > Network to File > Cytoscape JSON (*.cyjs)).
Note: the documentation indicates that I can use cy.json() to export a JSON representation of the graph, but I am unsure how to call this command (I am relatively new to javascript).
You just have to call the cytoscape object .json() for taking a json object and then download it as you like
here there is an example. I think StackOverflow is blocking downloading file but if you copy and paste this code it should be work
document.addEventListener("DOMContentLoaded", function() {
var cy = (window.cy = cytoscape({
container: document.getElementById("cy"),
style: [{
selector: "node",
style: {
content: "data(id)"
}
},
{
selector: "edge",
style: {
"curve-style": "bezier",
"target-arrow-shape": "triangle"
}
}
],
elements: {
nodes: [{
data: {
id: "a"
}
}, {
data: {
id: "b"
}
}],
edges: [{
data: {
id: "ab",
source: "a",
target: "b"
}
}]
},
layout: {
name: "grid"
}
}));
cy.ready(function() {
const json = cy.json(); // take json from cytoscape
// download json as you want
const data = "text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(json));
const a = document.createElement('a');
a.href = 'data:' + data;
a.download = 'data.json';
a.innerHTML = 'download JSON';
const container = document.getElementById('container');
container.appendChild(a);
});
});
body {
font-family: helvetica neue, helvetica, liberation sans, arial, sans-serif;
font-size: 14px
}
#cy {
position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 0;
z-index: 1;
}
h1 {
opacity: 0.5;
font-size: 1em;
font-weight: bold;
}
<head>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
<script src="https://unpkg.com/cytoscape/dist/cytoscape.min.js"></script>
</head>
<body>
<div id="cy"></div>
<div id="container" style="position:absolute;top:10;left:10;z-index:1"></div>
</body>
I have a mongo collection where documents have aprox the following structure:
item{
data{"emailBody":
"{\"uniqueKey\":\" this is a stringified json\"}"
}
}
What I want to do is to use 'uniqueKey' as an indexed field, to make an "inner join" equivalant with items in a different collection.
I was thinking about running a loop on all the documents -> parsing the json -> Saving them as new property called "parsedEmailBody".
Is there a better way to handle stringified json in mongo?
The only way is to loop through the collection, parse the field to JSON and update the document in the loop:
db.collection.find({ "item.data.emailBody": { "$type": 2 } })
.snapshot().forEach(function(doc){
parsedEmailBody = JSON.parse(doc.item.data.emailBody);
printjson(parsedEmailBody);
db.collection.updateOne(
{ "_id": doc._id },
{ "$set": { "item.data.parsedEmailBody": parsedEmailBody } }
);
});
For large collections, leverage the updates using the Bulk API:
var cursor = db.collection.find({ "item.data.emailBody": { "$type": 2 } }).snapshot(),
ops = [];
cursor.forEach(function(doc){
var parsedEmailBody = JSON.parse(doc.item.data.emailBody);
ops.push({
"updateOne": {
"filter": { "_id": doc._id },
"update": { "$set": { "item.data.parsedEmailBody": parsedEmailBody } }
}
});
if (ops.length === 500) {
db.collection.bulkWrite(ops);
ops = [];
}
});
if (ops.length > 0) { db.collection.bulkWrite(ops); }
I have json data which is displayed on an html page javascript, I was wondering if there was a way to make this information clickable like a link. Anyone know how this would be done?
Here is my json, I want to pick up each name from the json file and make it clickable.
{
"example": [
{
"name": "Dr. Sammie Boyer",
"email": "Lavonne.Kiehn#hotmail.com"
},
{
"name": "Eladio Beier",
"email": "Lavonne.Kiehn#hotmail.com"
},
{
"name": "Hilton Borer",
"email": "Reva.Goyette#yahoo.com"
}
]
}
Code I tried
$(document).ready(function() {
$.getJSON('example.json', function(data) {
var output = '';
$.each(data.name, function(key, value) {
output += '<a href=' + value.name + '</a>';
});
$('#user').html(output);
});
});
Try this (updated) - Set the value of the href as you loop
$.getJSON('example.json', function(data) {
var output = '';
$.each(data.name, function(key, value) {
output += '' + value.name + '';
});
$('#user').html(output);
});
just replace emailto with mailto to be like this
$.getJSON('example.json', function(data) {
var output = '';
$.each(data.name, function(key, value) {
output += '' + value.name + '';
});
$('#user').html(output);
});
I have a flash app where in a function I have to parse a json passed like an object by some external API that I can't change.
my json look like this:
{
"prodotti": [
{
"titolo": "test",
"marca": "",
"modello": "",
"cilindrata": "",
"potenza": "",
"alimentazione": "",
"images": {
"img": [
{
"thumb": "admin/uploads/img_usato/small/qekabw95L5WH1ALf6.jpg",
"big": "admin/uploads/img_usato/big/qekabw95L5WH1ALf6.jpg"
},
{
"thumb": "admin/uploads/img_usato/small/default.jpg",
"big": "admin/uploads/img_usato/big/default.jpg"
}
]
}
},
{
"titolo": "Motore Volvo TAMD 74 C",
"marca": "VOLVO PENTA",
"modello": "TAMD 74 C",
"cilindrata": "7.283 cm3",
"potenza": "331 kW a 2600 rpm",
"alimentazione": "Gasolio",
"images": {
"img": [
{
"thumb": "admin/uploads/img_usato/small/PmQwN4t4yp7P1YCWa.jpg",
"big": "admin/uploads/img_usato/big/PmQwN4t4yp7P1YCWa.jpg"
},
{
"thumb": "admin/uploads/img_usato/small/BWkjTGcy3pDM2LKRs.jpg",
"big": "admin/uploads/img_usato/big/BWkjTGcy3pDM2LKRs.jpg"
}
]
}
}
]
}
I want to parse the images inside the object.
The API send me an object not astring or json and I have this function now:
function changeData (prodotto:Object) {
img_container.graphics.clear ();
//here I want to enter and take thumb and big of images!!!
for (var index in prodotto.images.img) {
//trace('index: ' + index);
//trace("thumb: " + index.thumb + ' big: ' + index.big);
}
descrizione.htmlText = prodotto.testo_html;
titolo.text = prodotto.titolo;
alimentazione.text = prodotto.alimentazione;
potenza.text = prodotto.potenza;
cilindrata.text = prodotto.cilindrata;
modello.text = prodotto.modello;
marca.text = prodotto.marca;
}
The function works fine but not for the for loop where I try to take the bug and thumb of my json how can I retrieve this information in this object?
Thanks
I think there is something wrong with how you are setting up the call back but since you didn't show code for the api we can't fix that, plus you stated you have no control over it.
No matter what the issue is it just does not seem correct.
I put together a function that will get all the thumbs and bigs.
You did not state otherwise.
function changeData (prodotto:Object) {
for each(var item in prodotto.prodotti){
trace('')
//trace(prodotto.testo_html);
trace(item.titolo);
trace(item.alimentazione);
trace(item.potenza);
trace(item.cilindrata);
trace(item.modello);
trace(item.marca);
for each( var imgs in item.images.img) {
trace('thumb',imgs.thumb)
trace('big',imgs.big)
}
}
}
I think you need to use a JSON parser. Use the one from this link: https://github.com/mikechambers/as3corelib
1: Add the com folder to your project directory or add it to your default class path.
2: Adapt this code to your liking. I am not sure how you're getting a literal object from the API. It really should just be a string unless you're using some sort of AMF. Regardless...
import com.adobe.serialization.json.*;
var data:String = '{"prodotti":[{"titolo":"test","marca":"","modello":"","cilindrata":"","potenza":"","alimentazione":"","images":{"img":[{"thumb":"admin/uploads/img_usato/small/qekabw95L5WH1ALf6.jpg","big":"admin/uploads/img_usato/big/qekabw95L5WH1ALf6.jpg"},{"thumb":"admin/uploads/img_usato/small/default.jpg","big":"admin/uploads/img_usato/big/default.jpg"}]}},{"titolo":"Motore Volvo TAMD 74 C","marca":"VOLVO PENTA","modello":"TAMD 74 C","cilindrata":"7.283 cm3","potenza":"331 kW a 2600 rpm","alimentazione":"Gasolio","images":{"img":[{"thumb":"admin/uploads/img_usato/small/PmQwN4t4yp7P1YCWa.jpg","big":"admin/uploads/img_usato/big/PmQwN4t4yp7P1YCWa.jpg"},{"thumb":"admin/uploads/img_usato/small/BWkjTGcy3pDM2LKRs.jpg","big":"admin/uploads/img_usato/big/BWkjTGcy3pDM2LKRs.jpg"}]}}]}';
function changeData(data)
{
img_container.graphics.clear();
var obj = JSON.decode(data);
for (var i:int = 0; i < obj.prodotti.length; i++)
{
for (var k in obj.prodotti[i].images.img)
{
trace("Thumb:",obj.prodotti[i].images.img[k].thumb);
trace("Big:",obj.prodotti[i].images.img[k].big);
}
descrizione.htmlText = obj.prodotti[i].testo_html;
titolo.text = obj.prodotti[i].titolo;
alimentazione.text = obj.prodotti[i].alimentazione;
potenza.text = obj.prodotti[i].potenza;
cilindrata.text = obj.prodotti[i].cilindrata;
modello.text = obj.prodotti[i].modello;
marca.text = obj.prodotti[i].marca;
}
}
changeData(data);
I am trying to parse a local JSON file that has been stored in the applicationDataDirectory. Im just needing to grab items from this file. Running V.2 of titanium
The JSON data structure looks like this
{
"object_name": [
{
"title": "Burrton",
"value": "Burrton",
"homeof": "Chargers",
"logo": "images/logos/BURRTON.jpg",
"colors": "#cccccc;#a9a9a9",
"links": {
"tsr": "http://www.schurzdigital.com/mfeeds/CollectionFeed.php?site=http%3A%2F%2Fwww.catchitkansas.com&collection=cik_burrton_headlines&title=cik_burrton_headlines&limit=25",
"sports": "www.schurzdigital.com/mfeeds/CollectionFeed.php?site=http%3A%2F%2Fwww.catchitkansas.com&collection=cik_burden_headlines&title=cik_burden_headlines&limit=25",
"videos": "http://www.schurzdigital.com/mfeeds/TividFeedConvert.php?site=http%3A%2F%2Fwww.catchitkansas.com&slug=e9c0b075-3230-4a45-803e-7ccb4b7f754e&title=Top%20videos&limit=25",
"photos": "http://serve.a-feed.com/service/getFeed.kickAction?feedId=1014509&as=8768",
"stats": {
"boys": {
"football": "http://stats.catchitkansas.com/report_fb-schedule_results.php?sport=15&team=763",
"basketball": "http://stats.catchitkansas.com/report_baskb-schedule_results.php?sport=26&team=2946",
"cross country": "http://stats.catchitkansas.com/report_cc-schedule_results.php?sport=13&team=764",
"golf": "http://stats.catchitkansas.com/report_golf-schedule_results.php?sport=16&team=767",
"track": "http://stats.catchitkansas.com/report_trackfield-schedule_results.php?sport=32&team=2948"
},
"girls": {
"volleyball": "http://stats.catchitkansas.com/report_vb-schedule_results.php?sport=22&team=766",
"basketball": "http://stats.catchitkansas.com/report_baskb-schedule_results.php?sport=27&team=2947",
"cross country": "http://stats.catchitkansas.com/report_cc-schedule_results.php?sport=13&team=764",
"golf": "http://stats.catchitkansas.com/report_golf-schedule_results.php?sport=17&team=768",
"track": "http://stats.catchitkansas.com/report_trackfield-schedule_results.php?sport=32&team=2948"
}
}
}
}
]
}
The Code im using to parse the file.
var fileName = 'school_select.json';
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, fileName);
if (file.exists()) {
var json = file.read();
var jsonObject = JSON.parse(json);
one.text = jsonObject.object_name[0].title;
two.text = jsonObject.object_name[0].homeof;
}
I answered my own question. I used the following code.
var fileName = 'school_select.json';
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, fileName);
var json, object_name, locatie, i, row, title, value, homeof, logo, colors, link;
var preParseData = (file.read().text);
var response = JSON.parse(preParseData);
Ti.API.info('title = ' + response.object_name[0].title);