I've got an API that returns the following:
{
"data": {
"columns": [
"epoch_timestamp_millieseconds",
"cpu_used_percent"
],
"values": [
[
1615230210000,
28.24
],
...
I'm able to get the second metric using the following three lines of code:
<#assign metricvalue = jsonObj.data.values[0]>
<#assign arr = metricvalue[1]>
&value=${arr}
&value would equal 28.24.
Is there a way to combine these into one line of code?
I'm looking for something like this:
&value=jsonObj.data.values[0].[1]
The issue is the [1] doesn't have a label
Your code is almost correct. Just a small correction. Remove the . between [0] and [1]. You don't need to use . to specify the index. You only need to use it to specify the property/key.
The code should be like
jsonObj.data.values[0][0] // 1615230210000
jsonObj.data.values[0][1] // 28.24
Sure! You were very close. Just remove the dot:
const response = {
"data": {
"columns": [
"epoch_timestamp_millieseconds",
"cpu_used_percent"
],
"values": [
[
1615230210000,
28.24
]
]
}
};
console.log(response.data.values[0][1]);
You can use destructuring as well:
const response = {
"data": {
"columns": [
"epoch_timestamp_millieseconds",
"cpu_used_percent"
],
"values": [
[
1615230210000,
28.24
]
]
}
};
const { data: { values: [[_, target]] } } = response;
console.log(target)
Related
I am trying to create postman tests for an API.
The response I am getting from post man is in a json form. It looks like an Object of arrays.
{
"389": [
[
"2021-04-30T00:00:00Z",
16.130089309443093
]
],
"390": [
[
"2021-04-30T00:00:00Z",
14.899161948201808
]
],
"391": [
[
"2021-04-30T00:00:00Z",
17.495245579925736
]
],
"392": [
[
"2021-04-30T00:00:00Z",
16.78176061001777
]
],
"393": [
[
"2021-04-30T00:00:00Z",
25.473437964096448
]
],
"394": [
[
"2021-04-30T00:00:00Z",
56.746358310562826
]
],
"388": [
[
"2021-04-30T00:00:00Z",
18.49559245290604
]
]
}
I am trying to test the integer value that comes after the date is greater than 0 but cant seem to figure out how to traverse the structure in javascript.
With normal response Jsons they usually have the ID beside them and you can use that value, but not with this response
This is the test so far
pm.test("Check performance > 0", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.greaterThan(0);
});
It seems you are testing the whole JSON to be > 0. You should loop throught the values :
pm.test("Check performance > 0", function () {
var jsonData = pm.response.json();
Object.values(jsonData).forEach(record => {
pm.expect(record[0][1]).to.greaterThan(0);
});
/* record will be equal to
[
[
"2021-04-30T00:00:00Z",
16.130089309443093
]
]
and then
[
[
"2021-04-30T00:00:00Z",
14.899161948201808
]
]
and so on */
});
Object.values retrieves the values of a dictionary. See Object.values
(if we want the keys ["389", "390", ...] we can use Object.keys)
I want to get a specific array based on a code, the array is something like this:
const arr = [
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=11f18ac1-0476-4a1e-ada6-09e6566abc19",
1595619171842,
"0b7ad06f-7776-4bab-a8c6-53fd5fd5bd9b"
],
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=b64c143d-e817-434f-bf6f-0bd0e8d9e7b5",
1595619171844,
"2f44a130-71d9-47ce-b5d5-04587c3c81fc"
],
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=71dc5d26-75f4-4141-905e-074b0705eac4",
1595619171845,
"d7eb2a05-1f5a-48dd-b7ac-f3b071499d00"
],
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=d3645614-0ea3-4d17-80ab-57c6c6525fab",
1595619171846,
"940fb9a7-6fdd-4f8b-a808-26a9c60114bf"
]
];
How to I get the array with code "d7eb2a05-1f5a-48dd-b7ac-f3b071499d00"?
I was using reduce to get the more recent image, but now I have no idea!
Array#find returns the first elmenent of an array that returns true for the given function.
const specificArray = arr.find(subArray => {
return subArray[2] === "d7eb2a05-1f5a-48dd-b7ac-f3b071499d00";
}
Array#find (with destructuring) is best suited for this purpose.
const res = arr.find(([,,code])=>code==="d7eb2a05-1f5a-48dd-b7ac-f3b071499d00");
const arr = [
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=11f18ac1-0476-4a1e-ada6-09e6566abc19",
1595619171842,
"0b7ad06f-7776-4bab-a8c6-53fd5fd5bd9b"
],
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=b64c143d-e817-434f-bf6f-0bd0e8d9e7b5",
1595619171844,
"2f44a130-71d9-47ce-b5d5-04587c3c81fc"
],
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=71dc5d26-75f4-4141-905e-074b0705eac4",
1595619171845,
"d7eb2a05-1f5a-48dd-b7ac-f3b071499d00"
],
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=d3645614-0ea3-4d17-80ab-57c6c6525fab",
1595619171846,
"940fb9a7-6fdd-4f8b-a808-26a9c60114bf"
]
];
const res = arr.find(([,,code])=>code==="d7eb2a05-1f5a-48dd-b7ac-f3b071499d00");
console.log(res);
I have a tricky question... I have an array looks like this:
[
[ [ 'Attribute1', 'Attribute1Synonym1' ], [ 'Attribute2' ] ],
[ [ 'Attribute3' ] ],
[ [ 'Attribute2' ] ]
]
My result should be:
[
'Attribute1 Attribute2',
'Attribute1Synonym1 Attribute2',
'Attribute3',
'Attribute2'
]
The tricky thing is:
the result array has to grouped by the sub-sub-array
the crux is, the first index is an array(1) of arrays(2) of arrays(3)
and i would to like flatten the array by level 3 (array(3)) and at the result should be every possible combination between the upper level.
At level 2 (the first index) is an array with ('Attribute1' and 'Attribute1Synonym1')
so the result should be:
'Attribute1 Attribute2'
and
'Attribute1Synonym1 Attribute2'
the 'Attribute2' comes from the upper level
if the second index of level 2 ['Attribute2'] has also multiple indexes
for example ['Attribute2Synonym5']
the result should be:
'Attribute1 Attribute2'
'Attribute1Synonym1 Attribute2'
'Attribute1 Attribute2Synonym5'
'Attribute1Synonym1 Attribute2Synonym5'
and so on...
This works against your provided example, but I'm going to guess it's fragile against more complex arrays:
const deep = [ [ [ 'Attribute1', 'Attribute1Synonym1' ], [ 'Attribute2' ] ],
[ [ 'Attribute3' ] ],
[ [ 'Attribute2' ] ] ];
const flat = [];
deep.forEach(element => {
const left = element[0];
const right = element[1];
left.forEach(leftElement => {
if(right){
right.forEach(rightElement => {
flat.push(leftElement + ' ' + rightElement);
});
} else {
flat.push(leftElement);
}
})
});
Maybe like this:
var input_arr=[ [ [ 'Attribute1', 'Attribute1Synonym1' ], [ 'Attribute2' ] ],
[ [ 'Attribute3' ] ],
[ [ 'Attribute2' ] ] ];
var output_arr=[];
for(var key1 in input_arr){
var sub_input_arr=input_arr[key1];
for(var key2 in sub_input_arr){
var sub_sub_input_arr=sub_input_arr[key2];
for(var key3 in sub_sub_input_arr){
output_arr.push(sub_sub_input_arr[key3]);
}
}
}
console.log(output_arr);
I'm trying to get an array filled with the info back from some requests made to different REST APIs.
I thought about using Promise.all to do that but for some reason, it yields an Array with a bunch of undefined inside.
[ undefined, undefined, undefined, undefined ]
Here's my code:
var _ = require("lodash");//Used exclusively to check if the result from the request is an object
var ccxt = require("ccxt");//External library used to make the requests
let pairs = ["ETH/EUR", "BTC/EUR", "LTC/EUR", "BCH/EUR"]; //Array on which the Promise.all is based
function test(p) {
for (var i = 0; i < ccxt.exchanges.length; i++) { //Looping through all the rest APIs
let exchange = new ccxt[ccxt.exchanges[i]](); //Defining each API to make the requests
if (exchange.hasFetchOrderBook) {
exchange //Beginning of the request
.fetchOrderBook(p)
.then(order => {
if (_.isObject(order) && order.bids[0][1]) {
let now = Math.floor(new Date());
order.mkt = exchange.name;
order.pair = p;
order.ping = now - order.timestamp;
return order; //Return the result of the request
}
})
.catch(e => {});
}
}
}
Promise.all(pairs.map(test)) //Making the requests based on the Pairs Array
.then(res => {
console.log(res); //Logging the results ==> [undefined, undefined, undefined, undefined] for some reason...
})
.catch(e => {
console.log(e);
});
I know that the requests are correctly being made since if I console.log the order within the loop, I get the correct results -- Example of the result when logging:
{ bids:
[ [ 12009.52, 0.0468 ],
[ 12008.5, 0.0227 ],
[ 12007.48, 30.9321 ],
[ 12006.46, 0.0537 ],
[ 12005.45, 0.0157 ],
[ 12004.43, 7.1659 ],
[ 12003.41, 0.0164 ],
[ 12002.39, 23.4159 ],
[ 12001.38, 0.0284 ],
[ 12000.36, 0.0132 ],
[ 11999.34, 0.0194 ],
[ 11998.33, 0.0034 ],
[ 11997.31, 7.526 ],
[ 2445.72, 34.075 ],
[ 2445.17, 25.4842 ],
[ 2444.96, 0.1118 ],
[ 2444.75, 23.288 ],
[ 2444, 0.0247 ],
[ 2443.8, 0.192 ],
[ 765.51, 0.0828 ] ],
asks:
[ [ 12048.74, 2.523 ],
[ 12049.77, 0.0159 ],
[ 12050.79, 0.029 ],
[ 12051.82, 0.0061 ],
[ 12052.84, 0.0181 ],
[ 12053.87, 0.0164 ],
[ 12054.89, 0.0355 ],
[ 12055.92, 0.0042 ],
[ 13419.62, 0.0063 ],
[ 13420.64, 0.0174 ],
[ 13421.78, 0.0143 ],
[ 13422.92, 0.026 ],
[ 13424.06, 0.0055 ],
[ 13425.2, 14.4552 ],
[ 13426.23, 0.0065 ],
[ 13427.25, 0.0057 ],
[ 13428.39, 0.0147 ],
[ 13429.53, 4.0375 ],
[ 13430.56, 23.9541 ],
[ 13431.58, 0.0137 ] ],
timestamp: 1512845715447,
datetime: '2017-12-09T18:55:15.447Z',
mkt: 'LakeBTC',
pair: 'BTC/EUR',
ping: 0 }
So I guess that the problems I'm dealing with has to do with the asynchronous character of the function... but I'm not sure how I can make it synchronous.
Again, just to try to clarify my question: The objective is to get an array with 4 different types of object (one per pair --> array) so that I can operate on each.
Just to make it clearer, here's an illustration of what I'm trying to achieve:
[
[
Object1,
Object2,
Object3,
etc...
],
[
Object1,
Object2,
Object3,
etc...
],
[
Object1,
Object2,
Object3,
etc...
],
[
Object1,
Object2,
Object3,
etc...
]
]
Why is Promise.all returning the array without waiting on the requests'results?
I hope that was clear enough! If not let mw know! :P
Thanks in advance for your help!
Your test function does return a undefined. You need to return a promise for the result:
function test(p) {
return Promise.all(ccxt.exchanges.map(api => { //Looping through all the rest APIs
//^^^^^^^^^^^^^^^^^^
let exchange = new ccxt[api](); //Defining each API to make the requests
if (exchange.hasFetchOrderBook) {
return exchange //Beginning of the request
.fetchOrderBook(p)
.then(order => {
if (_.isObject(order) && order.bids[0][1]) {
let now = Math.floor(new Date());
order.mkt = exchange.name;
order.pair = p;
order.ping = now - order.timestamp;
return order; //Return the result of the request
}
// else undefined
})
.catch(e => {}); // undefined
}
// else undefined
}));
}
Of course your promises still fulfill with undefined when the if conditions do not apply or an error happens.
I've been working on this for a while now and I can't seem to figure it out. I have a JSON that I am pulling in from a link, and I'm trying to create a filter so that only names that start with a certain keyword show up.
I've been trying to use .parseJson and also I've tried str.includes() but I can't seem to get either working right.
Here's a piece of what the JSON looks like:
[
{
"max_percentile_90th": 42142.1,
"daily_percentile_50th": 21334.5,
"timerName": "Booklet:Landscape",
"sparks": [
26651,
23801.2,
20850.8,
19157.4,
21198.5,
],
},
{
"max_percentile_90th": 59253.4,
"daily_percentile_50th": 9040.5,
"timerName": "Search:Blog",
"sparks": [
9248.1,
13653,
8277,
9532.2,
10912.4,
],
},
{
"max_percentile_90th": 16707.9,
"daily_percentile_50th": 3793,
"timerName": "Search:Records:Download",
"sparks": [
6257.6,
8269,
9395.4,
6211.4,
9420.7,
],
},
{
"max_percentile_90th": 6979.4,
"daily_percentile_50th": 2821,
"timerName": "Search:AfterEffects",
"sparks": [
3752.2,
4522.8,
6167.2,
3847.9,
4568.9,
],
},
{
"max_percentile_90th": 5900.8,
"daily_percentile_50th": 2323,
"timerName": "Booklet:Geneologies",
"sparks": [
3359.2,
3317.8,
3113.5,
2839,
2675.6,
],
}
]
So as you can see, the timerName STARTS with "Booklet" (but had different things after it) on two of them and starts with "Search" on three of them. The JSON is much longer, but this snippet will get my point across. So I want to make a filter that will only show the ones that start with "Search" or only show the ones that start with "Booklet". I need all the data associated with it though, because I'm making a table, so is it possible to make a new JSON based on that? Or how should I go about this?
Here is a fiddle from what I've tried: http://jsfiddle.net/xjcfw7zf/
Any help is much appreciated! Thank you!!
To filter objects that start with "Booklet" can be achieved by following
jsonData = jsonData.filter(function(obj){
return (obj.timerName.indexOf("Booklet") == 0)
});
For reference - http://jsfiddle.net/xjcfw7zf/3/
Note - To be more generic, you can have a variable as well in place of "Booklet"
Use .filter() and .indexOf. Filter will iterate through each object and filter based on a condition. indexOf will tell if the given key is present in the string.
function search(data, key) {
return data.filter(function(item) {
return item.timerName.indexOf(key) == 0;
});
}
var jsonData = [{
"max90th": 42142.1,
"daily90th": 21334.5,
"timerName": "Booklet:Landscape",
"sparks": [
26651,
23801.2,
20850.8,
19157.4,
21198.5,
],
}, {
"max90th": 59253.4,
"daily90th": 9040.5,
"timerName": "Search:Blog",
"sparks": [
9248.1,
13653,
8277,
9532.2,
10912.4,
],
}, {
"max90th": 16707.9,
"daily90th": 3793,
"timerName": "Search:Records:Download",
"sparks": [
6257.6,
8269,
9395.4,
6211.4,
9420.7,
],
}, {
"max90th": 6979.4,
"daily90th": 2821,
"timerName": "Search:AfterEffects",
"sparks": [
3752.2,
4522.8,
6167.2,
3847.9,
4568.9,
],
}, {
"max90th": 5900.8,
"daily90th": 2323,
"timerName": "Booklet:Geneologies",
"sparks": [
3359.2,
3317.8,
3113.5,
2839,
2675.6,
],
}];
function search(data, key) {
return data.filter(function(item) {
return item.timerName.indexOf(key) == 0;
});
}
alert(search(jsonData, "Booklet").length);
alert(search(jsonData, "Search").length);
alert(search(jsonData, "Something").length);
How about iterating over the main array in your JSON object using a for loop, perfoming a comparison using substring on the current iteration object's timerName property, and then putting that into a result array on match, only to finally return that array?
/**
* #param {array} json
* #param {string} filter
*/
function itemsByTimerName(json, filter) {
var filtered = [];
for (var i = 0; i < json.length; ++i) {
// if 'timerName' begins with 'filter'
if (json[i].timerName.substring(0, filter.length) === filter) {
filtered.push(json[i]);
}
}
return filtered;
}
Use a library such as lodash, to filter based on which ever key and value you are trying to find, in lodash you would want to use "pluck" for what you are after. Lodash is a very useful library which enables you to do all sorts of things with JSON objects, and javascript in general.
https://lodash.com/docs#filter