Problems getting specific data out of response from ajax call - json

The json file I am calling can be found here. It contains population data for regions in Ghent (a city in Belgium) during a specific year. I use the following code to retrieve the data, the array where i store the response data is called "inwonersperwijk":
function(key,value) {
$.each(key,function(key,value){
inwonersperwijk.push(value);
});
$.each(key[0], function (key, value) {
years.push(key);
});
}
,
error: function(er){
console.log(er);
}});
which returns an object that looks like this.
What I am trying to do is retrieve the "wijk" in each of these objects. When I try to use inwonersperwijk[0].year_1999 for example, it returns the corresponding data just fine.
But when I want to retrieve the "wijk" part of the data by using inwonersperwijk[0].wijk, I get an undefined. Could anyone possibly help me out with this?

Looking at your JSON source file I can see that the indexes for the years are "year_1999", "year_2000" and so on, but the index for the wijk is actually "\ufeffwijk". The \ufeff is a non-breaking space (see here).
If you look at your output array screenshot, you will see that the year indices have no quotes, while the index for wijk does (it's "wijk" not wijk). This is why you get undefined: the index for wijk does not actually exist.
You're better off first filtering the \ufeff characters out of the JSON before parsing it.

Related

Get data from array in JSON API object in Cypress

Relative newbie to Cypress and JSON data. I have an api online that I can access. The api has data similar to this:
{"record":[{"account":"acount_1","team":"Test 1","req_id":12345},{"account":"acount_2","team":"Test 2","req_id":23456}],"metadata":{"id":"abcde","private":false,"createdAt":"2022-12-21T00:00:00.000Z"}}
I am attempting to find a manner to get the amount of records that are in the api, as well as get the first team name.
The closest I have come to getting any kind of data is by using something like this:
cy.get('#testing').then((data) => {
for (let index in data)
cy.log(data[index])
})
However, all that does is show me what is in the API, not the data in the array itself. I have attempted dozens of different options, none of which has worked. I hope someone can please help me!
Assuming your intercept was waited on with the alias and the data is nested as you say in the respon, you can access the response JSON data
cy.get('#testing')
// get records and check length
.its('record')
.should('have.length', 2)
// get first team name
.its('0.team')
.should('eq', 'Test 1')
Here is a working example.

Using Flow to return a SharePoint list to Powerapps

I've used this Flow to return the count of any Sharepoint list to Powerapps.
https://masteroffice365.com/get-sharepoint-library-or-list-total-items-from-power-apps/
How would I modify it to return the contents of a list to Powerapps, so that I can use Powerapps to put it into a collection?
Would this mean I don't have to worry about Delegation if the list has more than 2000 items?
This is what I've tried so far.
There is a variable TotalItemsCount which I have changed to ListItems. Instead of using an Integer I set ListItems to an array.
In the Get Library list contents I use this for the URI.
concat( '_api/web/lists/GetbyTitle(''', first( body('Filter_Library_List_Being_Queried') )?['displayName'], ''')/Items' )
I'm not sure what to put in as the last step given that I want it to be able to return the contents of any list. I think this rules out a parse json step as that requires a definite schema.
I've added an ApplyToEach
I'm getting this error message when it runs.
ExpressionEvaluationFailed. The execution of template action
'Apply_to_each' failed: the result of the evaluation of 'foreach'
expression '#body('Get_Library_List_Contents')' is of type 'Object'.
The result must be a valid array.
I don't think you can return an array back to PowerApps. You would have to return the response as a JSON string, then have your PowerApp do the logic to convert the JSON string into a collection.
Likely your PowerApp would have to include something like this to convert the JSON string that's returned from the flow:
ClearCollect(
*collectionName*,
MatchAll(
*JSON_String*,
*"\{""date"":""(?<date>[^""]*)"",""message"":""(?<message>[^""]*)"",""user"":""(?<user>[^""]*)""\}"*
));
Flow returning response body to PowerApp
If I understand you correctly, you want to retrieve 14000 records from the Sharepoint list, and not just the total count.
Would this mean I don't have to worry about Delegation if the list has more than 2000 items?
Yes, when you use a cloud flow rather than directly accessing Sharepoint list from Powerapps, you basically avoid delegation of 2k records.
Now coming back to you main topic of retrieving Records, you would have to Test and run your flow and check what does the below http return. I believe it returns a JSON Array.
concat( '_api/web/lists/GetbyTitle(''', first( body('Filter_Library_List_Being_Queried') )?['displayName'], ''')/Items' )
You would have to apply a for each or clean your JSON output to return String Array or JSON Array as output of your all 14K Records.
In addition if you are using Sharepoint online why not use connector for flow mentioned here

Accessing information in a JSON nested in object

I'm trying to access the roomName, but so far I am unable. I don't get how to get past the barriar of the info.[long ID with dashes].roomName.
At most I can get back the object of the long id or undefined.
I have tried info[0].roomName. Trying to get the first object in the info and then go on from there. The long id number is also in list.id, I don't know if that can help.
I would have set the info as an array like list is, but this is not my JSON, only one that I am working with.
{
"list":[ IGNORE, can access code here ],
"info":{
"e5eb1ccf-bd45-4d01-8e2a":{
"id":"e5eb1ccf-bd45-4d01-8e2a",
"name":"Lucy",
"roomName":"Arts" <<I need to get to this.
}
}
}
I hope this makes sense, first post and this is just a boiled down version of what I have. Putting in the id number in the info.e5eb1ccf-bd45-4d01-8e2a.roomName breaks after the first -
Once you've parsed the JSON (assuming you even have JSON*) and you have an object, you'd use brackets notation:
var room = theObject.info["e5eb1ccf-bd45-4d01-8e2a"].roomName;
console.log(room); // Arts
* Remember, JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON.

JSON oData.metadata

I have questions about JSON returning from the server using the Microsoft oData API.
Cannot figure it out.
Query1:
http://localhost:63717/odata/City(1)
Fiddler returns the raw data below.
Everything is in its own brackets.
{
"odata.metadata":"http://localhost:63717/odata/$metadata#City/#Element","CityID":1,"CityName":"Minnetonka","CityAddr1":null,"CityAddr2":null,"CityCity":null,"CityState":null,"CityZip":null,"CityPhone":null,"CityFAX":null,"CityExtent":"-93.53,44.88,-93.39,44.93","CityHeaderImage":null
}
Query2:
http://localhost:63717/odata/City?$filter=CityName eq 'Minnetonka'
Fiddler returns the raw data below.
Data is in two sets of bracketed data
{
"odata.metadata":"http://localhost:63717/odata/$metadata#City","value":[
{
"CityID":1,"CityName":"Minnetonka","CityAddr1":null,"CityAddr2":null,"CityCity":null,"CityState":null,"CityZip":null,"CityPhone":null,"CityFAX":null,"CityExtent":"-93.53,44.88,-93.39,44.93","CityHeaderImage":null
}
]
}
What do I have to do to format my JSON coming back for $filters in the oData request?
That odata.metadata is killing me in Query2.
Please explain what I am doing wrong.
In the first example, you have just one City element (denoted by City(1) in the request and #City/#Element in the result path).
In the second example, the value property in result is showing an array of City types (a listing of one or more objects). [ ... ] denotes an array in JavaScript. For a $filter type query, this is what I would expect. You can also see that the response path is less specific (#City instead of #City/#Element).
The path shown in the odata.metadata property value describes the structure of the element being returned, as I showed two examples above. The format of the return data will change depending on how you request it.
If you're having trouble parsing the JSON returned, consider using a library to do the heavy lifting for you. For example:
datajs
JayData
Breeze.js
[Source]
You are not doing anything wrong, the two formats actually represent two different forms of result.
The first you are requesting a single item as you are specifying the key for the entity.
In the second you are potentially asking for a list of entities. The Odata.Metadata is separate in this response otherwise it would be repeated for every item returned and would be a waste in terms of content length.
Because of the way that you are addressing the entity.
With //localhost:63717/odata/City(1) you are addressing one entity ("/entityset/key"). You will always return back one City (if one exists). There is no need for it to return an array because it will never return more than one.
With //localhost:63717/odata/City you are addressing a collection of entities ("/entityset"). 0 to n City entities could be returned, hence the need for a collection.

reading a json file and selecting values with dojo

I'm trying to read a json file and select a value in the file, but my googling skills have failed me.
I've come across dojo.xhrGet & ItemFileReadStore, but I'm not sure which is the correct one to use. Or are neither correct?
Any help or wave of a flashlight in the right direction would be greatly appreciated.
Can you be more specific. What do you mean by select a values in file? Using dojo you can perform all range of HTTP request(GET, POST, PUT etc) and specify if returened data is text or json.
xhr.get({
url:"data.json",
handleAs:"json",
load: function(data){
for(var i in data){
console.log("key", i, "value", data[i]);
}
}
});
Here data can be treated as object and based on key data can be retrived using obj.key notation