I have the following string(with new lines after each value):
"
value1
value2
value3
"
I need to convert it to a json array(using python) in the following format:
{"keywords": [{"keyword": "value1"},{"keyword": "value2"},{"keyword": "value3"}]}
I tried all kinds of methods, always resulting in invalid json.
Any suggestions?
Split the string into an array, map it to a dictionary of keyword:line, and convert it into json.
import json
def map_line(line):
return {"keyword": line}
lines = "value1\nvalue2\nvalue3".split("\n")
result = json.dumps({"keywords": list(map(map_line, lines))})
import json
sample_data = '{"keywords": [{"keyword": "value1"},{"keyword": "value2"},{"keyword": "value3"}]}'
for idx, key in enumerate(json.loads(sample_data)):
for idx2, key2 in enumerate(json.loads(sample_data)[key]):
print(key2)
Output:
{'keyword': 'value1'}
{'keyword': 'value2'}
{'keyword': 'value3'}
I having a trouble with a code in MAYA 2020.
I want to create a new expression into a default value of a Arnold aiUserDataInt
I need the default value of a aiUserDataInt = attribute from a geometry that I crate name "ID"
so, my code looks like this:
string $Selected[] = `ls -selection`;
for ($node in $Selected)
aiUserDataInt1.default = $Selected.id;
but I have these error:
// Error: Line 2.37: Cannot cast data of type string[] to float. //
So, I suppose default value do not accept arrays
my question would be: is there a way to convert array into float?
or what am I doing wrong?
Thanks in advance
Unfortunately that's not how mel works. You can do:
string $Selected[] = `ls -selection`;
for ($node in $Selected)
{
int $id = getAttr($node + ".id");
setAttr("aiUserDataInt1.default", $id);
}
Didn't test it, but it should work like this. You get and set attributes with getAttr() and setAttr().
I get data from a URL, and am working on the data to check for a few conditions. The data from the URL look like this:
1528190345":100,"1528190346":100,"1528190368":100,"1528190414":100,"1528190439":99,"1528190440":99,"1528190463":100,"1528190485":100,"1528190508":100,"1528190550":100,"1528190575":100,"1528190576":100,"1528190599":100,"1528190600":100,"1528190622":100,"1528190667":100,"1528190688":100,"1528190689":100,"1528190712":100,"1528190736":100,"1528190762":100,"1528190785":100,"1528190786":100,"1528190807":100,"1528190828":100,"1528190853":100,"1528190877":100,"1528190901":100,"1528190925":100,"1528190948":100,"1528190968":100,"1528190991":100}}]
====
I have converted that too JSON
{"metric"=>"Insta_real-unique_value", "tags"=>{"host"=>"letme.quickly.com", "tier"=>"2", "device"=>"tester1", "dc"=>"xxx"}, "aggregateTags"=>["device_name", "device_ip"], "dps"=>{"1526972408"=>100, "1526972424"=>100, "1526972440"=>100, "1526972456"=>100, "1526972472"=>100, "1526972488"=>100, "1526972504"=>100, "1526972520"=>100, "1526972536"=>100, "1526972552"=>100, "1526972568"=>100, "1526972569"=>100, "1526972584"=>100, "1526972585"=>100, "1526972601"=>100, "1526972617"=>100, "1526972633"=>100, "1526972649"=>100, "1526972665"=>100, "1526972681"=>100}}
I want to extract the value that corresponds to 100. When I do this:
url = "#{URL}"
uri = URI(url)
response = Net::HTTP.get(uri)
value = response[-6..-4]
puts value
I get the last value, but when the last value changes to 99/9/0, it prints :99 or ":9.
Is there a way to get the exact value as is?
When dealing with JSON data, it's almost always better to parse the data properly rather than using regex against the string.
In this case, we can do:
JSON.parse(response)['dps'].values.last #=> 100
If the response is a json response, you must use a json parser else if is not a json response, you can use a regex expression with a Regex Object.
In case of a json response, assuming that the object is something like is declared into the variable response of the next code, you can parse it into a JObject. (using Newtonsoft.Json available from nuget repository).
See the next example :
string response = "[{\"response\":{\"1528190345\":100,\"1528190346\":100,\"1528190368\":100,\"1528190414\":100,\"1528190439\":99,\"1528190440\":99,\"1528190463\":100,\"1528190485\":100,\"1528190508\":100,\"1528190550\":100,\"1528190575\":100,\"1528190576\":100,\"1528190599\":100,\"1528190600\":100,\"1528190622\":100,\"1528190667\":100,\"1528190688\":100,\"1528190689\":100,\"1528190712\":100,\"1528190736\":100,\"1528190762\":100,\"1528190785\":100,\"1528190786\":100,\"1528190807\":100,\"1528190828\":100,\"1528190853\":100,\"1528190877\":100,\"1528190901\":100,\"1528190925\":100,\"1528190948\":100,\"1528190968\":100,\"1528190991\":100}}]";
List<Dictionary<string, Dictionary<string, int>>> values = JsonConvert.DeserializeObject<List<Dictionary<string, Dictionary<string, int>>>>(response);
Dictionary<string, Dictionary<string, int>> firstLevel = values[0]; // Access to the first object of the list closed with ']'
Dictionary<string, int> secondLevel = firstLevel["response"]; // Access to the first object response and get's it's object context of first '}' starting from the end of response
/** This is an option, if you ever knows the name of the element (1528190991) */
int thirdLevel = secondLevel["1528190991"]; // Access to the last element of the object by it's name, context of second '}' starting from the end of response.
Console.WriteLine(thirdLevel);
/** This is another option if you doesn't know the name of the element and wants ever the last element. */
List<int> listOfValues = secondLevel.Values.ToList();
Console.WriteLine(listOfValues[listOfValues.Count-1]);
Note that i've chenged a little bit your response adding [{\"response\":{\" at the start to become a json response.
If is not a json response you can use this pattern with regular expression :
:(.{2,6})}}\]$
Hope will help!
I am having the following error: htmlentities() expects parameter 1 to be string, array given when I am executing the following sql statement in laravel.
public function index($id)
{
$event = Task::find($id);
//$tickremain = DB::select(DB::raw("SELECT artists FROM tasks where id='1'"));
$tickremain = DB::table('tasks')->select(DB::raw('count(*) as thresholdc'))->get();
return view('buy.index', ['event' => $event],['tickremain'=>$tickremain]);
}
My view:
<p>No. of Tickets remaining: {{$tickremain}}</p>
I want to get a count of a column field but I'm left with that error.
Use {{$tickremain->thresholdc}} to print the count.
You are getting this array because $tickremain is an array. htmlentities() format is
string htmlentities ( string $string ).
So just try to print $tickremain and check whether it is array or not. If it is array just access like
$tickremain['thresholdc']
(based on array)
I have an array loaded in, and been playing around in the REPL but can't seem to get this to work.
My array looks like this:
record_id|string|FALSE|1|
offer_id|decimal|FALSE|1|1,1
decision_id|decimal|FALSE|1|1,1
offer_type_cd|integer|FALSE|1|1,1
promo_id|decimal|FALSE|1|1,1
pymt_method_type_cd|decimal|FALSE|1|1,1
cs_result_id|decimal|FALSE|1|1,1
cs_result_usage_type_cd|decimal|FALSE|1|1,1
rate_index_type_cd|decimal|FALSE|1|1,1
sub_product_id|decimal|FALSE|1|1,1
campaign_id|decimal|FALSE|1|1,1
When I run my command:
for(i <- 0 until schema.length){
val convert = schema(i).toString;
convert.split('|').drop(2);
println(convert);
}
It won't drop anything. It also is not splitting it on the |
Strings are immutable, and so split and drop don't mutate the string - they return a new one.
You need to capture the result in a new val
val split = convert.split('|').drop(2);
println(split.mkString(" "));
Consider also defining a lambda function for mapping each item in the array, where intermediate results are passed on with the function,
val res = schema.map(s => s.toString.split('|').drop(2))