Extracting Data from non-JSON API url - javascript

I would to set expectations first. I know basic HTML, and understand CSS, vbScript and JavaScript but not an expert, not even intermediate.
I want to extract data from an API url that looks like this...
and the browser returns these data...
Domain;Rank;Organic Keywords;Organic Traffic;Organic Cost;Adwords Keywords;Adwords Traffic;Adwords Cost
somedomain.com;31970;26015;35679;252734;0;0;0
How can I get these data into a JSON file so I can create a JavaScript for it.
Thanks!

You can split the String, then assign the parts to an object manually:
var response = "Domain;Rank;Organic Keywords;Organic Traffic;Organic Cost;Adwords Keywords;Adwords Traffic;Adwords Cost;somedomain.com;31970;26015;35679;252734;0;0;0";
var fields = ["Dn", "Rk", "Or", "Ot", "Oc", "Ad", "At", "Ac", null, "v1", "v2", "v3", "v4"];
var data = response.split(";");
var parsedData = {};
fields.forEach(function(fld, i) {
if (fld) parsedData[fld] = data[i];
});
console.log(parsedData);

http://codepen.io/anon/pen/yavrYj?editors=1111
split by the semicolon, break them up into two arrays (key, value) then make the json object
var data = "Domain; Rank; Organic Keywords; Organic Traffic;Organic Cost;Adwords Keywords;Adwords Traffic; Adwords Cost; somedomain.com;31970;26015;35679;252734;0;0;0"
var values = data.split(";")
var half = values.length / 2
var keys = values.splice(0,half);
var jsonData = {};
for (i = 0; i < keys.length; i++) {
jsonData[keys[i]] = values[i]
}
the result is
Object {
Adwords Cost: "0",
Organic Keywords: "26015",
Organic Traffic: "35679",
Rank: "31970",
Adwords Keywords: "0",
Adwords Traffic: "0",
Domain: " somedomain.com",
Organic Cost: "252734"
}
you had asked in your comment how do you get your data from the api?
If you are using jquery ajax I guess it would be something like
$(function() {
$.ajax("http://api.domain.com/?type=domain_rank&key=somekey&export_columns=Dn,Rk,Or,Ot,Oc,Ad,At,Ac&domain=somedomain..com&database=us",
{
type: 'get',
cache: false,
dataType: 'text',
contentType: "application/json",
success: function (data) {
var values = data.split(";")
var half = values.length / 2
var keys = values.splice(0,half);
var jsonData = {};
for (i = 0; i < keys.length; i++) {
jsonData[keys[i]] = values[i]
}
},
error: function (xhr, status) {
console.log(status);
console.log(xhr.responseText);
}
});
});
however if you are attempting to call this api from javascript on a different domain you may run into CORS problems.

Related

POSTMAN - How to set environment value for nested object in single request?

Im trying to sign few fields and send the signed field as another field in an request.that is inside of an nested object..Im trying to setEnvironmentvariable but it is setting the same value again..
{
"id" : "1234",
"ref" : "REF05",
"details": [
{
"traceNumber": "201902120005",
"amount": "100",
"customerNumber": "0924",
"signature": "{{signature}}"
},
{
"traceNumber": "201902120007",
"amount": "120",
"customerNumber": "0739",
"signature": "{{signature}}"
}
],
"date": "20210619"
}
Im signing traceNumber+amount+customerNumber fields by private key and passing the value to signature fields which works fine with single object. if i try with multiple objects it just set the last calculated signature.
if (globals['jsrsasign-js'] === undefined) {
console.log("jsrasign library not already downloaded. Downloading now. ")
pm.sendRequest({
url: "http://kjur.github.io/jsrsasign/jsrsasign-latest-all-min.js",
method: "GET",
body: {}
}, function(err, res) {
postman.setGlobalVariable("jsrsasign-js", res.text());
doHttpSig();
});
} else {
doHttpSig();
}
function doHttpSig() {
var navigator = {}; //fake a navigator object for the lib
var window = {}; //fake a window object for the lib
eval(postman.getGlobalVariable("jsrsasign-js"));
let pk = postman.getGlobalVariable("PRIVATEKEY")
let pks = pk.split(/\\n/)
//console.log("pkss Key", pks);
privateKey = ""
for (let i = 0; i < pks.length; i++) {
privateKey += pks[i] + "\n"
}
var reqBody = JSON.parse(request.data);
for (let i = 0; i<reqBody.details.length; i++) {
var fields = reqBody.ref+reqBody.details[i].traceNumber+reqBody.details[i].amount+reqBody.details[i].customerNumber;
var signature_algo = "SHA1withDSA";
var payload = fields;
var sig = new KJUR.crypto.Signature({
"alg": signature_algo
});
sig.init(privateKey);
sig.updateString(payload);
var sigValueHex = sig.sign();
var sigBase64 = hextob64(sigValueHex);
postman.setEnvironmentVariable("signature", sigBase64);
}
}
This is my postman prescript. some one pls let know hoe to acheive this.
I want to add SettlementValues object in the Environment variable and when I add, I see the value as [object,Objet] instead of complex object.
var settlementValues = new Object();
var requestData = pm.request.body.raw;
var requestjson=JSON.parse(requestData);
settlementValues.TransactionCode=requestjson.ProcessingCode.TransactionCode;
settlementValues.TransactionAmount= requestjson.TransactionAmount.Amount;
settlementValues.ReferenceNumber=requestjson.RetrievalReferenceNumber;
settlementValues.BanknetReferenceNumber=requestjson.NormalizedData.MastercardData.BanknetReferenceNumber;
console.log(settlementValues);
pm.environment.set("SETTLEMENTOBJECT",settlementValues);

Get JSON array data by field name

I'm trying to get JSON data by field name like this data.name and it return the desired data, but I have 25 fields in the array and I want to make this dynamically, using data + "." + variable, when I alert it returns [Object object].name, so how I can make it executable?
I tried many ways but all failed, please help me doing this.
$.ajax({
type: "Get",
url: "/Home/Report_Data",
datatype: "json",
dataSrc: "",
contentType: 'application/json; charset=utf-8',
data: {
'Arrnagement': Arrnagement
},
success: function(data) {
var result = getElementsById("LD_name LD_Loan_Type LD_id LD_Full_Name_AR LD_GENDER LD_BIRTH_INCORP_DATE LD_PS_MOTHER_NAME LD_Street_AR LD_TEL_MOBILE LD_EMPLOY_STATUS_D LD_EMPLYRS_Name LD_MARITAL_STATUS LD_PS_PL_OF_BIR_AR LD_wifeName LD_Effective_Interest_Rate LD_Contract_amount LD_Repayment_Amount LD_Sector_name LD_NUM_REPAYMENTS LD_Loan_Maturity LD_Orig_Contract_Date LD_Loan_CCY LD_Arrangement LD_COLLATERAL_TYPE LD_Description LD_COLLATERAL_VALUE LD_COLLATERAL_Currency LD_GUARANTOR_ID LD_NATIONALITY LD_G_Full_Name_En LD_G_DATE_OF_BIRTH LD_G_PLACE_OF_BIRTH LD_G_MOTHER_NAME_EN LD_HOUSING_LOAN_AREA_CLASS LD_HOUSING_PROPERTY_NATURE LD_HOUSING_LOAN_PURPOSE LD_HOUSING_PROPERTY_AREA");
var jid;
for (var i = 0; i < result.length; i++) {
jid = (result[i].id.substring(3));
var resulting = data[0].jid;
alert(resulting);
if (result[i].innerHTML = data[0].jid != "undefined") {
result[i].innerHTML = data[0].jid;
} else {
result[i].innerHTML = "";
}
}
//jid = name;
//data[0].name returns "Joun"
//data[0]+"."+jid returns [object object].name but i need it to return "Joun"
This should work. I changed the dot notation while accessing the object property.
$.ajax({
type: "Get",
url: "/Home/Report_Data",
datatype: "json",
dataSrc: "",
contentType: 'application/json; charset=utf-8',
data: { 'Arrnagement': Arrnagement },
success: function (data) {
var result = getElementsById("LD_name LD_Loan_Type LD_id LD_Full_Name_AR LD_GENDER LD_BIRTH_INCORP_DATE LD_PS_MOTHER_NAME LD_Street_AR LD_TEL_MOBILE LD_EMPLOY_STATUS_D LD_EMPLYRS_Name LD_MARITAL_STATUS LD_PS_PL_OF_BIR_AR LD_wifeName LD_Effective_Interest_Rate LD_Contract_amount LD_Repayment_Amount LD_Sector_name LD_NUM_REPAYMENTS LD_Loan_Maturity LD_Orig_Contract_Date LD_Loan_CCY LD_Arrangement LD_COLLATERAL_TYPE LD_Description LD_COLLATERAL_VALUE LD_COLLATERAL_Currency LD_GUARANTOR_ID LD_NATIONALITY LD_G_Full_Name_En LD_G_DATE_OF_BIRTH LD_G_PLACE_OF_BIRTH LD_G_MOTHER_NAME_EN LD_HOUSING_LOAN_AREA_CLASS LD_HOUSING_PROPERTY_NATURE LD_HOUSING_LOAN_PURPOSE LD_HOUSING_PROPERTY_AREA");
var responseData = data[0];
var jid;
for (var i = 0; i < result.length; i++) {
jid = (result[i].id.substring(3));
var resulting = responseData[jid];
alert(resulting);
if (responseData[jid]) {
result[i].innerHTML = responseData[jid];
}
else {
result[i].innerHTML = "";
}
}
Try giving data[0][jid], we can give a variable in brackets also inorder to get the data
Hope it works
If do foo.bar you are getting a property with the name 'bar' on object foo. If you have some variable const bar = "qux" and you want to access property on some object with the same name as bar value /"qux"/ you just need to use square brackets - foo [bar], which will be the same as calling foo.qux; So, in your case you just need to use data[0][jid] instead of data [0].jid, supposing jid contains a string that is also a key in data[0].
You can just do data[0][variableName] and this will return the data you want. for example. If data had a json string [{ "Name" : "Jane Doe"}] You could execute it like this.
var variableName = "Name";
console.log(data[0][variableName])
This would return "Jane Doe".
if you have your field names in an array you can loop through them using $.each or a for loop.
For example say your json string is [{"First_Name" : "Jane", "Last_Name" : "Doe", "Age" : 32}] you could get all the values from the json string doing this.
var FieldNames = ["First_Name" , "Last_Name", "Age"]
$.each(FieldNames, function(i,item) {
console.log(data[0][item])
}
OR
var FieldNames = ["First_Name" , "Last_Name", "Age"]
for(var i = 0; i < FieldNames.length; i++) {
console.log(data[0][FieldNames[i]])
}

JSON.stringify and $http request

I have a JS String, like so:
user_fav = "21,16";
This has to go thru a function where it becomes a JSON array with an id key, like so:
{"id":21},{"id":16}
And this goes into an $http request:
return $http({
method: 'GET',
url: getUrl('products'),
params: {
pageSize: 2000,
pageNumber: 1,
"filter": { "q": { "$or": [{"id":21},{"id":16}] } }, // <-- HERE
sort: []
}
});
Now if I run the above $http request everything works fine, but if I convert the String (user_fav) into that JSON and send this to the $http request it fires an error. This is my converter:
user_fav = "21,16";
var user_fav_to_array = "";
var splitFav = user_fav.split(",");
for (var i = 0; i < splitFav.length; i++) {
user_fav_to_array += JSON.stringify({ id: parseInt(splitFav[i]) }) + ',';
}
var JSONFavs = user_fav_to_array.substring(0, user_fav_to_array.length - 1);
//Result: JSONFavs => {"id":21},{"id":16}
So this gives an error:
return $http({
method: 'GET',
url: getUrl('products'),
params: {
pageSize: 2000,
pageNumber: 1,
"filter": { "q": { "$or": [JSONFavs] } }, // <-- HERE
sort: []
}
});
Madame and messier the error is 417 (Critical Exception), this is coming from the Backand.com syste
Have a look at the code below. What your code actually doing is assigning a Json STRING to $or property. But really needed is to assign array of items (as expected). Let me know if you have any questions
user_fav = "21,16";
//Dont need this
//var user_fav_to_array = "";
// New Variable to save array items
var arrayData =[];
var splitFav = user_fav.split(",");
for (var i = 0; i < splitFav.length; i++) {
// don't need this
//user_fav_to_array += JSON.stringify({ id: parseInt(splitFav[i]) }) + ',';
// Just create a simple javascript object with id and put it into array at i index
arrayData[i]={ id: parseInt(splitFav[i]) };
}
//var JSONFavs = user_fav_to_array.substring(0, user_fav_to_array.length - 1);
// Then in Http Request filter, it should be just like this "$or": arrayData
"filter": { "q": { "$or": arrayData } }
This might have to deal with your variable - var user_fav_to_array = "";
You may want to change it to the following var user_fav_to_array = [];
Since it has the quotations it may be causing your variable to behave in a way that you don't want it to. Unless strings are treated as arrays in javascript.

Getting data from JSON Structure

My coldfusion component is returning a JSON format data. I am trying to access the values of that data in my front-end using Javascript. Can someone help me understand how to access the data values such as the "id", "firs_name" and "last_name"?
I am storing the the follow data in the variable called tempData. Below is the JSON structure I am getting:
{ "COLUMNS" : [ "id",
"FIRST_NAME",
"LAST_NAME"
],
"DATA" : [ [ "xxxx",
"Jes",
"Abr"
],
[ "xxx2",
"JESSIE",
"YU"
]
]
}
Below is my ajax call:
$.ajax({
type: "get",
url: "GROUPSLIST.cfc",
data: {
method: "getNames",
queryString: selectQuery
},
success: function(a) {
alert(a);
},
error: function(a) {
alert(a.responseText);
}
});
I am assuming your response is not parsed already. You can use something like following.
var tempData = '{"COLUMNS":["id","FIRST_NAME","LAST_NAME"],"DATA":[["xxxx","Jes","Abr"],["xxx2","JESSIE","YU"]]}';
//parse response if not already parsed
var respObj = JSON.parse(tempData);
var columns = respObj['COLUMNS'];
//create a column map to index like following
var colMap = {};
for(i = 0; i < columns.length; i++){
colMap[columns[i]] = i;
}
console.log(colMap)
var data = respObj['DATA'];
var text = ''
//use data[i][colMap['id']] to access data inside loop.
for(i = 0; i < data.length; i++){
text += data[i][colMap['id']] + ':' +data[i][colMap['FIRST_NAME']] + ' ' + data[i][colMap['LAST_NAME']] + '<br>';
}
document.getElementById('text').innerHTML = text;
<div id="text"></div>
This might be also help full while using DB query and access by column(ex:employee_id,employee_name)
Step-1
// return in serializeJSON format with true
<cffunction name="employeelistJson" returntype="any" returnformat="JSON">
<cfquery name="employeelist" datasource="yourdatasource">
select * from employee
</cfquery>
<cfset setJson = #serializeJSON(employeelist,true)#>
<cfreturn setJson />
</cffunction>
Step-2 access json data by name
success: function(response) {
//must be in caps
alert(response.DATA['EMPLOYEEID']);
alert(response.DATA['EMPLOYEENAME']);
},

How to add items to an empty array in Javascript

Controller sends a JSON for one Frame. I need to keep incrementing the array where Multiple Frame's score gets added. For example
FrameScore = f1 + f2 +.. lastF
Issue The values do not get added and shows data for each Frame only. Where am I doing it wrong?
var bowlingData = {
"frames": []
};
var frames = [];
$('#submitButton').click(function(e) {
frames.push([$("#FirstRoll").val(), $("#SecondRoll").val()]);
for (var ln = 0; ln < frames.length; ln++) {
var temp = {
"firstroll": $("#FirstRoll").val(),
"secondroll": $("#SecondRoll").val()
};
bowlingData.frames.push(temp);
}
console.log("temp data: " + temp);
bowlingData.frames.push(temp);
var element = this;
$.ajax({
url: "/Home/Submit",
type: "POST",
data: JSON.stringify(bowlingData),
dataType: "json",
traditional: true,
contentType: "application/json; charset=utf-8",
success: function(data) {
var parseData = JSON.parse(data);
console.log("MyDate: " + data.score);
console.log("Parse" + parseData);
$("#lblTotalScore").text(parseData.score);
$("#FirstRoll").val("");
$("#SecondRoll").val("");
},
error: function() {
alert("An error has occured!!!");
}
});
});
Apparently the solution is
var frameArray= [];
frameArray.push("#FirstRoll");
frameArray.push("#SecondRoll");
But that is to add single elements to the array. If the input is [[2, 3],[4, 5],...] then the JSON object representation would be
{ "frames": [{"first": 2, "second": 3}, {"first": 4, "second": 5}, ... ] }
However, there was another issue of not getting the correct response from the controller.
The issue here is that an empty array is created (i.e. frames) and on the 3rd line the value was pused to the empty Array. Although the the for loop was adding each element to the Array(i.e. frames) but when the response was created the recent input was replacing the previous input, because the JSON object bowlingData was holding temp data only. So no need to create any Array to increment multiple input result. Initial value would be hold by the browser and second input would be added in next submit.
Was
var frames = [];
$('#submitButton').click(function(e) {
frames.push([$("#FirstRoll").val(), $("#SecondRoll").val()]);
for (var ln = 0; ln < frames.length; ln++) {
var temp = {
"firstroll": $("#FirstRoll").val(),
"secondroll": $("#SecondRoll").val()
};
bowlingData.frames.push(temp);
}
Should be
$('#submitButton').click(function (e) {
bowlingData.frames.push({
"firstroll": $("#FirstRoll").val(),
"secondroll": $("#SecondRoll").val()
});

Categories

Resources