.getjson from external domain. Print from the Array - jsonp

I'm currently getting 2 arrays from the following get statement.
$.getJSON("http://domain.com?callback=?", function(data){
$("#results").html(JSON.stringify(data));
I'm appending the results in a div with the id results.
All that is doing is just displaying the array. I need to pull certain objects from the array but I can't do that because I am just printing the whole array. How do I get specific information from that array?
Here is some of the data that is being appended to the DIV.
{"me":[{"player":{"high_score":110345,"rank":2}}],"all":[{"player":{"#score := s.score":110345,"avatar":"http://profile.ak.fbcdn.net/hprofile-ak-snc6/195312_789328764_1121893995_q.jpg","guid":"FE4EC535-B74F-4B68-8F4D-2CA0EBC28FAF","name":"Charles Chase","rank":1,"score":110345}},
Thanks!

Provided that the following is your returned JSON data
{
"me": [
{
"player": {
"high_score": 110345,
"rank": 2
}
}
],
"all": [
{
"player": {
"#score := s.score": 110345,
"avatar": "http://profile.ak.fbcdn.net/hprofile-ak-snc6/195312_789328764_1121893995_q.jpg",
"guid": "FE4EC535-B74F-4B68-8F4D-2CA0EBC28FAF",
"name": "Charles Chase",
"rank": 1,
"score": 110345
}
}
]
};
You can use the following to grab the values from the JSON object
CODE
$.getJSON("http://domain.com?callback=?", function(data){
alert(data.me[0].player.high_score);
alert(data.all[0].player.name);
));
Note me and all is returned as array. I hope it helps.

Related

restrict collection.find() should only return non-empty object but not empty objects inside array

const response = await simpleSurveyResponsesModel
.find({
"metaKey.projectId": 7,
})
.select([
"answers.SEC.text",
"answers.S4.text",
"answers.S3_a.text",
"answers.Urban_City.text"
])
.lean();
Where answers feild is array of objects containing both empty ad non-empty objects.
collection.find() returns the selected objects but also empty objects as well, inside answers array which i dont need.
My Output
{
"_id": "61840979fcbea215bc61ed03",
"answers": [
{},
{},
{
"Urban_City": {
"text": "Hyderabad"
}
},
]
}
Expected Output
{
"_id": "61840979fcbea215bc61ed03",
"answers": [
{
"Urban_City": {
"text": "Hyderabad"
}
},
]
}

How to get data from nested array in DataTables?

I used to download data from a json file in this format to my DataTables table:
{
"data": [
["n/a","668","01.11.2021 14:16:20", ... ],
["n/a","670","05.11.2021 23:23:54", ...]
]
}
...
"ajax": "first.json",
columns: [
{ data: 0 },
{ data: 1 }
...
And everythig was ok
But now format of my json was changed:
{
"data": {
"deals": [["n/a", "718", "30/11/2021 21:46:14"], ["", "718", "30/11/2021 21:46:14"], ... ],
"stops": [["07/10/2021 21:48:28", "BTCUSDT"], ["07/10/2021 21:48:28", "BTCUSDT"], ... ]
}
}
And I try to get data like this and get "No data available in table":
...
"ajax": "first.json",
columns: [
{ data: 'deals.0' },
{ data: 'deals.1' }
...
How can I get data from new format of json to my table?
Here is your new JSON structure, provided by the URL:
{
"data": {
"deals": [
["n/a", "718", "30/11/2021 21:46:14"],
["", "718", "30/11/2021 21:46:14"]
],
"stops": [
["07/10/2021 21:48:28", "BTCUSDT"],
["07/10/2021 21:48:28", "BTCUSDT"]
]
}
}
In this structure, the location of the deals data is data.deals. This location points to an array of arrays, which is what DataTables needs (or an array of objects).
(This means the table will only have access to the deals data, since the stops data is in a separate location entirely. But I will assume you only want the deals data to match your original example.)
You therefore need to use the DataTables dataSrc option to tell DataTables where to look in your new JSON:
<table id="example" class="display" style="width:100%"></table>
and:
$(document).ready(function() {
$('#example').DataTable( {
ajax: {
method: "GET",
url: "first.json", // or whatever URL you want to use
dataSrc: "data.deals"
},
"columns": [
{ "title": "Col 1" },
{ "title": "Col 2" },
{ "title": "Col 3" }
]
} );
} );
Because each row of data is an array, you don't need to specify the specific array indexes in your columns - DataTables will iterate over each row array for you.
The result is:

how to select a single item and get it's relations in faunadb?

I have two collections which have the data in the following format
{
"ref": Ref(Collection("Leads"), "267824207030650373"),
"ts": 1591675917565000,
"data": {
"notes": "voicemail ",
"source": "key-name",
"name": "Glenn"
}
}
{
"ref": Ref(Collection("Sources"), "266777079541924357"),
"ts": 1590677298970000,
"data": {
"key": "key-name",
"value": "Google Ads"
}
}
I want to be able to query the Leads collection and be able to retrieve the corresponding Sources document in a single query
I came up with the following query to try and use an index but I couldn't get it to run
Let(
{
data: Get(Ref(Collection('Leads'), '267824207030650373'))
},
{
data: Select(['data'],Var('data')),
source: q.Lambda('data',
Match(Index('LeadSourceByKey'), Get(Select(['source'], Var('data') )) )
)
}
)
Is there an easy way to retrieve the Sources document ?
What you are looking for is the following query which I broke down for you in multiple steps:
Let(
{
// Get the Lead document
lead: Get(Ref(Collection("Leads"), "269038063157510661")),
// Get the source key out of the lead document
sourceKey: Select(["data", "source"], Var("lead")),
// use the index to get the values via match
sourceValues: Paginate(Match(Index("LeadSourceValuesByKey"), Var("sourceKey")))
},
{
lead: Var("lead"),
sourceValues: Var("sourceValues")
}
)
The result is:
{
lead: {
ref: Ref(Collection("Leads"), "269038063157510661"),
ts: 1592833540970000,
data: {
notes: "voicemail ",
source: "key-name",
name: "Glenn"
}
},
sourceValues: {
data: [["key-name", "Google Ads"]]
}
}
sourceValues is an array since you specified in your index that there will be two items returned, the key and the value and an index always returns the array. Since your Match could have returned multiple values in case it wasn't a one-to-one, this becomes an array of an array.
This is only one approach, you could also make the index return a reference and Map/Get to get the actual document as explained on the forum.
However, I assume you asked the same question here. Although I applaud asking questions on stackoverflow vs slack or even our own forum, please do not just post the same question everywhere without linking to the others. This makes many people spend a lot of time while the question is already answered elsewhere.
You might probably change the Leads document and put the Ref to Sources document in source:
{
"ref": Ref(Collection("Leads"), "267824207030650373"),
"ts": 1591675917565000,
"data": {
"notes": "voicemail ",
"source": Ref(Collection("Sources"), "266777079541924357"),
"name": "Glenn"
}
}
{
"ref": Ref(Collection("Sources"), "266777079541924357"),
"ts": 1590677298970000,
"data": {
"key": "key-name",
"value": "Google Ads"
}
}
And then query this way:
Let(
{
lead: Select(['data'],Get(Ref(Collection('Leads'), '267824207030650373'))),
source:Select(['source'],Var('lead'))
},
{
data: Var('lead'),
source: Select(['data'],Get(Var('source')))
}
)

express-graphql: How to remove external "data" object layer.

I am replacing an existing REST endpoint with GraphQL.
In our existing REST endpoint, we return a JSON array.
[{
"id": "ABC"
},
{
"id": "123"
},
{
"id": "xyz"
},
{
"id": "789"
}
]
GraphQL seems to be wrapping the array in two additional object layers. Is there any way to remove the "data" and "Client" layers?
Response data:
{
"data": {
"Client": [
{
"id": "ABC"
},
{
"id": "123"
},
{
"id": "xyz"
},
{
"id": "789"
}
]
}
}
My query:
{
Client(accountId: "5417727750494381532d735a") {
id
}
}
No. That was the whole purpose of GraphQL. To have a single endoint and allow users to fetch different type/granularity of data by specifying the input in a query format as opposed to REST APIs and then map them onto the returned JSON output.
'data' acts as a parent/root level container for different entities that you have queried. Without these keys in the returned JSON data, there won't be any way to segregate the corresponding data. e.g.
Your above query can be modified to include another entity like Owner,
{
Client(accountId: "5417727750494381532d735a") {
id
}
Owner {
id
}
}
In which case, the output will be something like
{
"data": {
"Client": [
...
],
"Owner": [
...
]
}
}
Without the 'Client' and 'Owner' keys in the JSON outout, there is no way to separate the corresponding array values.
In your case, you can get only the array by doing data.Client on the returned output.

How to iterate over json array

I have this json code
{
"apps": [
{
"com.eeenmachine.tinytowers": [
{
"text": "one"
},
{
"text": "two"
}
]
}
]
}
And i am trying to get all the text values.
Trying with this code
NSArray *titles = [jsArray[#"apps"] valueForKey:game];
NSLog(#"[DEBUG]titles %#",titles);
for (id obje in titles){
NSLog(#"[DEBUG]obje %#",obje);
NSLog(#"[DEBUG]obje_class %#",[obje class]);
}
Problem is that I don't get each text value , instead i get all the values.
Output:
obje (
{
text = one;
},
{
text = two;
}
)
Looking at your sample data there are a lot of arrays ([, ]) as well as dictionaries ({, }). For example the value for key apps is an array containing a single dictionary. Your code does not appear to be dealing with these extra levels.
HTH