I'm inheriting a chart that someone else has already written. I now want to add extra data to the series to be displayed in the tooltip.
The original code is setup with arrays or arrays to build the series data, like so:
$data['series'][$nextindex]['data'][] = array(strtotime($date) * 1000, $budgetSum);
When I dump the array the output is like so:
[
{
"name": "Adjusted Budget",
"color": "#7570B3",
"data": [
[
1486454400000,
0
],
[
1493622000000,
2200
],
[
1494226800000,
3700
],
....
I know to add extra data it needs to be formatted as a hash/object:
[
{x: 1486454400000, y: 0, myData: test1},
{x: 1493622000000, y: 2200, myData: test2},
{x: 1494226800000, y: 3700, myData: test3},
.....
]
I'm just not sure how to do the conversion...
I tried something like this (but it didn't work):
$myArr = array('x' => strtotime($date) * 1000, 'y' => $budgetSum, 'myData' => 'test1');
$data['series'][$nextindex]['data'][] = json_encode($myArr);
Nevermind, I was overthinking it.
I got it to work with:
$myArr = array('x' => strtotime($date) * 1000, 'y' => $budgetSum, 'myData' => 'test1');
$data['series'][$nextindex]['data'][] = $myArr;
No need for the json_encode()
Related
I'm trying to combine requests from two services in one endpoint.
The first returns the list of users, like this:
{
"data": [
{ "id": 1, "name": "first", "photo": "employee_photos/key1.png" },
{ "id": 2, "name": "second", "photo": null },
{ "id": 3, "name": "third", "photo": "employee_photos/key3.png" }
]
}
The second is supposed to receive the POST request with the JSON listing photo keys to get the required version URLs.
Request:
{
"keys": [ "employee_photos/key1.png", "employee_photos/key3.png" ],
"versions": [ "small", "large" ]
}
I created a small Lua script to process the response of the first request and collect the list of keys. It looks like this:
function post_employees(request, employeesResponse)
local resp = employeesResponse.load()
local data = resp:data():get("data")
local photo_keys = {}
for i = 0, data:len() - 1, 1 do
local rec = data:get(i)
local id = rec:get("id")
local photo = rec:get("photo")
if photo then
table.insert(photo_keys, photo)
end
end
resp:data():set("photos", table.concat(photo_keys, ","))
end
But then... I can't find a way to use this list of keys in the second request. Is it even possible?
I have a performance issue in my application. I would like to gather some ideas on what I can do to improve it. The application is very easy: I need to add values inside a nested table to get the total an user wants to pay out of all the pending payments. The user chooses a number of payments and I calculate how much it is they will pay.
This is what I have:
jsonstr = "{ "name": "John",
"surname": "Doe",
"pending_payments": [
{
"month": "january",
"amount": 50,
},
{
"month": "february",
"amount": 40,
},
{
"month": "march",
"amount": 45,
},
]
}"
local lunajson = require 'lunajson'
local t = lunajson.decode(jsonstr)
local limit -- I get this from the user
local total = 0;
for i=1, limit, 1 do
total = total + t.pending_payments[i].amount;
end;
It works. At the end I get what I need. However, I notice that it takes ages to do the calculation. Each JSON has only twelve pending payments (one per month). It is taking between two to three seconds to come up with a result!. I tried in different machines and LUA 5.1, 5.2., 5.3. and the result is the same.
Can anyone please suggest how I can implement this better?
Thank you!
For this simple string, try the test code below, which extracts the amounts directly from the string, without a json parser:
jsonstr = [[{ "name": "John",
"surname": "Doe",
"pending_payments": [
{
"month": "january",
"amount": 50,
},
{
"month": "february",
"amount": 40,
},
{
"month": "march",
"amount": 45,
},
]
}]]
for limit=0,4 do
local total=0
local n=0
for a in jsonstr:gmatch('"amount":%s*(%d+),') do
n=n+1
if n>limit then break end
total=total+tonumber(a)
end
print(limit,total)
end
I found the delay had nothing to do with the calculation in LUA. It was related with a configurable delay in the retrieval of the limit variable.
I have nothing to share here related to the question asked since the problem was actually in an external element.
Thank #lfh for your replies.
I need to remove the attribute nested1 that is inside the attr7_nested in a massive way, I don't need to persist this data in the bank, just store it in a variable to send to a log file.
example file follows:
[{
"attr1": 120,
"attr2": 24,
"attr3": 11400,
"attr4": "Caixa",
"attr5": 2000000,
"attr6": 1744000,
"attr7_nested": {
"nested1": 1,
"nested2": "Essential",
"nested3": "med",
"nested4": "Med"
}
},
{
"attr1": 120,
"attr2": 24,
"attr3": 11400,
"attr4": "Caixa",
"attr5": 2000000,
"attr6": 1744000,
"attr7_nested": {
"nested1": 1,
"nested2": "Ess",
"nested3": "med",
"nested4": "Med"
}
}]
When array is the array containing the nested hashes from your question than the following would remove the nested1 keys from all nested attr7_nested hashes:
array.each { |hash| hash[:attr7_nested].delete(:nested1) }
Given a hash that looks like this:
{
"one":[ [ 46, 51 ], [ 46 ], [ 48 ] ],
"two":[ [ 50, 51 ], [ 46, 51 ], [ 46, 51 ] ]
}
How do you map this in Rails so that we could get the total number of items in all arrays? So that we would get this result:
{
"one": 4,
"two": 6
}
I'm a little confused with using map, because it doesn't let me retain the keys.
You can use this to retain the keys as well
hash.map{|key, val| [key, val.flatten.count]}.to_h
Use Hash#transform_values:
hash.transform_values { |v| v.flatten.count }
#=> {:one=>4, :two=>6}
Generally you can simply use flatten to merge the nested arrays. So if you want to update your existing hash you can do
your_hash.each { |key, value| your_hash[key] = value.flatten.count }
Otherwise I'd say you do
new_hash = {}
your_hash.each { |key, value| new_hash[key] = value.flatten.count }
I'm using a Ruby script to interface with an application API and the results being returned are in a JSON format. For example:
{
"incidents": [
{
"number": 1,
"status": "open",
"key": "abc123"
}
{
"number": 2,
"status": "open",
"key": "xyz098"
}
{
"number": 3,
"status": "closed",
"key": "lmn456"
}
]
}
I'm looking to search each block for a particular "key" value (yzx098 in this example) and return the associated "number" value.
Now, I'm very new to Ruby and I'm not sure if there's already a function to help accomplish this. However, a couple days of scouring the Googles and Ruby resource books hasn't yielded anything that works.
Any suggestions?
First of all, the JSON should be as below: (note the commas)
{
"incidents": [
{
"number": 1,
"status": "open",
"key": "abc123"
},
{
"number": 2,
"status": "open",
"key": "xyz098"
},
{
"number": 3,
"status": "closed",
"key": "lmn456"
}
]
}
Strore the above json in a variable
s = '{"incidents": [{"number": 1,"status": "open","key": "abc123"},{"number": 2,"status": "open","key": "xyz098"},{"number": 3,"status": "closed","key": "lmn456"}]}'
Parse the JSON
h = JSON.parse(s)
Find the required number using map
h["incidents"].map {|h1| h1['number'] if h1['key']=='xyz098'}.compact.first
Or you could also use find as below
h["incidents"].find {|h1| h1['key']=='xyz098'}['number']
Or you could also use select as below
h["incidents"].select {|h1| h1['key']=='xyz098'}.first['number']
Do as below
# to get numbers from `'key'`.
json_hash["incidents"].map { |h| h['key'][/\d+/].to_i }
json_hash["incidents"] - will give you the value of the key "incidents", which is nothing but an array of hash.
map to iterate thorough each hash and collect the value of 'key'. Then applying Hash#[] to each inner hash of the array, to get the value of "key". Then calling str[regexp], to get only the number strings like '098' from "xyz098", finally applying to_i to get the actual integer from it.
If the given hash actually a json string, then first parse it using JSON::parse to convert it to a hash.Then do iterate as I said above.
require 'json'
json_hash = JSON.parse(json_string)
# to get values from the key `"number"`.
json_hash["incidents"].map { |h| h['number'] } # => [1, 2, 3]
# to search and get all the numbers for a particular key match and take the first
json_hash["incidents"].select { |h| h['key'] == 'abc123' }.first['number'] # => 1
# or to search and get only the first number for a particular key match
json_hash["incidents"].find { |h| h['key'] == 'abc123' }['number'] # => 1