reading a json file and selecting values with dojo - json

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

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.

Methods to convert CSV to unique JSON

I need to convert a .CSV to a specific .JSON format, and I decided to use the csvtojson package from NPM since it seemed to be designed for this sort of thing.
First, a little background on my problem. I have .CSV data that looks similar to this:
scan_type, date/time, source address, source-lat, source-lng, dest address, dest-lat, dest-lng
nexpose,2016-07-18 18:21:44,1008,40.585260,-10.124120,10.111.131.4,10.844880,-10.933360
I have a plain csv-to-json converter here , but there is a problem. It outputs a rather plain file, and I need to split the source/destination, and add some special formatting that I will show later on.
[
{
"scan_type": "nexpose",
"date/time": "2026-07-28 28:22:44",
"source_address": 2008,
"source-lat": 10.58526,
"source-lng": -105.08442,
"dest_address": "11.266.282.0",
"dest-lat": 11.83388,
"dest-lng": -111.82236
}
]
The first thing I need to do is be able to separate the "source" values, from the "destination" values. Here is an example of what I want the "source" values to look like:
(var destination will be exactly the same format)
var source={
id: "99.58926-295.09492",
"source-lat": 49.59926,
"source-lng": -209.98942,
"source_address": 2009,
x: {
valueOf: function() {
var latlng=[
49.58596,
-209.08442
];
var xy = map.FUNCTION_FOR_CONVERTING_LAT_LNG_TO_X_Y(latlng);
return xy[0]; //xy.x
},
y: {
valueOf: function(){
varlatlng=[
49.58596,
-209.08442
];
var xy = map.FUNCTION_FOR_CONVERTING_LAT_LNG_TO_X_Y(latlng);
return xy[1]; //xy.y
}
}
So, my question is, how should I approach converting my data? Should I convert everything with csvtojson? Or should I convert it from the plain .JSON file I generated?
Does anyone have any advice, or similar examples, they could share on how to approach this problem?
I do a lot of work with parsing CSV data and as I am sure you have seen, CSV is very hard to parse and work with correctly as there are a huge number of edge cases that can break even the most rugged of parsers (although it looks like your dataset is fairly plain so that isn't a huge concern). Not to mention you could potentially run into corruption by performing operations while reading from disk, so it is a much better idea to get the data from CSV into a JSON file and then make any manipulations to a JSON object loaded from that "plain" JSON file.
tl;dr: convert your data from the plain .JSON file

Problems getting specific data out of response from ajax call

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.

Convert JSON contents into HTML

This has probably been asked a load of times for, so forgive me for asking again.
I have a need to display the contents of a Json string as a formatted HTML fragment. It will be purely a read only view, the Json will vary as well.
I have seen modules out there that deal with form generation based of Json schemas but in my case there is no schema.
Is there anything out there anyone can recommend?
If you are looking for a library to use a quick google search found this.
www.json2html.com
I'm not sure if you are trying to use json data that pre-exists or starting from scratch as you did not provide an example but if you are starting from scratch this might be a good tool to design around.
This may be too limited, but if you're just looking for a pretty-printed view of your json, JSON.stringify supports that out of the box. I use this custom filter:
.filter('pretty', function () {
return function (json) {
return JSON.stringify(json, undefined, 2);
};
})
<pre ng-bind="myData | pretty"></pre>
I haven't come across anything standalone that does this in a generic sense... my approach has been to 'roll my own' so to speak and build to suit my needs like so:
$.each(retrievedStatusData, function(i, val){
$('ul').append('<li class="gamer-list-item">' + val.gamertagis + '</li>');
$('li').eq(i).prepend('<img src="' + val.gamercard.gamerpicSmallImagePath + '" class="gamer-pic">');
});
... which would produce an unordered list with the gamertag / name and a small thumbnail photo for each object in the JSON data source Im working with.
Now I HAVE on occasion, when inspecing JSON structures at a birds eye view sometimes done a loop over JSON and used sort of a for each key loop that displays ALL the JSON data in a 2 column table, the key on the left and value on the right. But that is only really useful for simple JSON data I guess.

JavaScript front-end and Progress4GL back-end

I want to create an application with front-end HTML + JavaScript and back-end Progress4GL.
I found this documentation: http://communities.progress.com/pcom/docs/DOC-106147 (see Introducing AJAX and Introducing JSON). In the example described it is used GET method when requesting data:
xmlhttp.open("GET", "http://localhost/cgi-bin/cgiip.exe/WService=wsbroker1/getcustomersJSON_param.p?piCustNum="+ custval, true);
xmlhttp.send();
and on Progress4GL procedure for getting the param it is used get-value("piCustNum").
In my application I want to use POST method. So the request will be, for example:
xmlhttp.open("POST","http://localhost/cgi-bin/cgiip.exe/WService=wsbroker1/getcustomersJSON_param.p",true);
xmlhttp.send("piCustNum=" + custval);
But I don't know how to get the sent param on Progress side. Actually I want to send a stringify JSON.
Can anyone help me with this? Thanks!
If you want to POST JSON data to a webspeed program, check out WEB-CONTEXT:FORM-INPUT or if you post more than 32K, check out WEB-CONTEXT:FORM-LONG-INPUT.
Now... regarding reading the JSON data, it depends on your OpenEdge version. In 10.2B Progress started supporting JSON, however it is very limited, especially if you have little control of how the JSON gets created. Since you are the one creating the JSON data it may work for you. Version 11.1 has much better support JSON including a SAX streaming implementation.
We were on version 10.2 so I had to resort to using this C library to convert the JSON into a CSV file. If you have access to Python on your server it is very easy to convert to a CSV file
For the front-end I'd recommend you to use some library (like jQuery) to handle the ajax's request for you, instead of dealing with the complexity to work with different browsers, etc. You can use jQuery's functions like $.ajax, $.get or $.post to make your requests.
A post to a webspeed page could easily be done like this:
var data = {
tt_param: [ { id: 1, des: 'Description 1' } ]
}
var params = { data: JSON.stringify(data) }
$.post(
'http://<domain>/scripts/cgiip.exe/WService=<service>/ajax.p',
params,
function (data) {
alert('returned:' + data);
},
'text'
);
And the back-end would receive the JSON string using get-value('data'):
{src/web2/wrap-cgi.i}
def temp-table tt_param no-undo
field id as int
field des as char.
def var lc_param as longchar no-undo.
procedure output-header:
output-content-type("text/text").
end.
run output-header.
assign lc_param = get-value('data').
temp-table tt_param:read-json('longchar', lc_param).
find first tt_param no-error.
{&OUT} 'Cod: ' tt_param.id ', Des: ' tt_param.des.
It's a good place to start, hope it helps.
Cheers,
There is a library from Node for calling Progress Business Logic dynamically. I hope this would help.
node4progress