So I am trying to find all elements within the json array below with an email with the domain example.com
JSON Array used:
[{"addedAt"=>123456,
"vid"=>123456,
"canonical-vid"=>123456,
"merged-vids"=>[],
"portal-id"=>123456,
"is-contact"=>true,
"profile-token"=>"portal-token-goes-here",
"profile-url"=>"portal-url-goes-here",
"properties"=>{"firstname"=>{"value"=>"Ramon"}, "lastmodifieddate"=>{"value"=>"12345"}, "company"=>{"value"=>"Some Company"}, "lastname"=>{"value"=>"Carleones"}},
"form-submissions"=>
[{"conversion-id"=>"some-convo-id-goes-here",
"timestamp"=>123456,
"form-id"=>"form-id-goes-here",
"portal-id"=>405406,
"page-url"=>"page-url-goes-here",
"content-type"=>"standard-page",
"page-title"=>"page-title-goes-here",
"title"=>"title-goes-here",
"first-visit-url"=>"first-visit-url-goes-here",
"first-visit-timestamp"=>123456,
"meta-data"=>[]}],
"identity-profiles"=>
[{"vid"=>123456,
"saved-at-timestamp"=>123456,
"deleted-changed-timestamp"=>0,
"identities"=>[{"type"=>"EMAIL", "value"=>"ramon#example.com", "timestamp"=>123456}, {"type"=>"Important", "value"=>"some-value", "timestamp"=>123456}]}],
"merge-audits"=>[]},
{"addedAt"=>123456,
"vid"=>123456,
"canonical-vid"=>123456,
"merged-vids"=>[],
"portal-id"=>123456,
"is-contact"=>true,
"profile-token"=>"portal-token-goes-here",
"profile-url"=>"profile-url-goes-here",
"properties"=>{"firstname"=>{"value"=>"Sally"}, "lastmodifieddate"=>{"value"=>"123456"}, "company"=>{"value"=>"Acme Inc."}, "lastname"=>{"value"=>"Jackson"}},
"form-submissions"=>
[{"conversion-id"=>"some-convo-id-goes-here",
"timestamp"=>123456,
"form-id"=>"some-form-id",
"portal-id"=>123456,
"title"=>"Big Freakin Title",
"meta-data"=>[]}],
"identity-profiles"=>
[{"vid"=>123456,
"saved-at-timestamp"=>123456,
"deleted-changed-timestamp"=>0,
"identities"=>[{"type"=>"EMAIL", "value"=>"sjackson#acme-example.com", "timestamp"=>123456}, {"type"=>"Team Lead", "value"=>"some-value", "timestamp"=>123456}]}],
"merge-audits"=>[]
}]
Here is the code I was trying:
I used factory girl and used an instance variable:
##json_array.contacts_obtained is a hash - contacts_obtained is the key, and the
#value is the actual json array above.
#json_array.contacts_obtained.detect do |i| i['identity-profiles'][0]['identities'][0]['value'] == /#example.com/
puts i
end
I wasn't sure if it was ok to do a regex there like that. It doesn't appear to work when I tried that. So I was wondering if there was another way to do this.
I am trying to obtain all the elements with #example.com domain so I can 'pop' them or remove them from the json array. Essentially I need a filter. That will filter out all the array elements with certain domain emails.
You are almost there. Use =~ instead of == iy ou want to match a string with a regexp:
#json_array.contacts_obtained.select do |i|
i['identity-profiles'][0]['identities'][0]['value'] =~ /#example.com/
end
Related
I'm working in a declarative pipeline, and I have a string that looks like this:
'[[key_A:value1, key_B:value2, key_C:value3],[key_A:value4, key_B:value5, key_C:value6],[key_A:value7, key_B:value8, key_C:value9]]'
Can I get help on what the quickest way is to turn the string into a map, then retrieve values of each map in the arraylist?
Can I get help on what the quickest way is to turn the string into a
map, then retrieve values of each map in the arraylist?
The input string you provide doesn't look like a map, it looks like a list of map. You can turn the string into a list of map using something like this (notice that the values here are quoted so they are strings, otherwise you would have to provide variables for value1, value2 etc):
def inputString = '[[key_A:"value1", key_B:"value2", key_C:"value3"],[key_A:"value4", key_B:"value5", key_C:"value6"],[key_A:"value7", key_B:"value8", key_C:"value9"]]'
def inputList = Eval.me (inputString)
Then you could iterate over that list to retrieve the maps and do whatever you want to do with the values in the maps:
def inputString = '[[key_A:"value1", key_B:"value2", key_C:"value3"],[key_A:"value4", key_B:"value5", key_C:"value6"],[key_A:"value7", key_B:"value8", key_C:"value9"]]'
def inputList = Eval.me (inputString)
inputList.each { Map m ->
println m.values()
}
I have an array (obj_values) of objects
[
#<User id: 1, name: "Kostas">,
#<User id: 2, name: "Moufa">,
...
]
And I want to convert this into an Array with only the values from above objects, so it will look like:
[
1, Kostas
2, Moufa
]
I can do it like this:
obj_table = []
obj_values.each do |ext|
ext.each do |obj|
obj_table.push([obj.id, obj.name].join(","))
end
end
However with this approach I need to explicitly specify what attributes I want to push (obj.id and obj.name), is there a way to push whole data from object without the need to specify each attribute separately?
Use .attributes to get a hash of all the attributes on the model. Call .values on it to get just the values without the keys.
Using your code:
obj_table = []
obj_values.each do |ext|
ext.each do |obj|
obj_table.push(obj.attributes.values.join(","))
end
end
Though there are better ways. I suggest you look into .map and .flat_map.
obj_table = obj_values.flat_map do |ext|
ext.map do |obj|
obj.attributes.values.join(",")
end
end
Yes, you can push data without specifying it separately. to push data you have to use an object.attributes.values
an object will be your active record object.
attributes will return a hash with all attributes of that object.
so you can change your loops into a single statement like:
obj_values.flat_map {|object_value| object_value.attributes.values }
I am new to Ruby.
I want to create a JSON file for a group of elements.
For this, I am using eachfunction to retrieve the datas. I want to create json as follows for the 4 length array,
'{
"desc":{
"1":"1st Value",
"2":"2nd value"
"3":"3rd Value",
"4":"4th value"
},
}'
This is my array iteration,
REXML::XPath.each( doc, "//time" ) { |element1|
puts element1.get_text
}
I know here is the simple code to generate a JSON,
require 'json/add/core'
class Item < Struct.new(:id, :name); end
chair = Item.new(1, 'chair')
puts JSON.pretty_generate(chair)
This syntax will generate a json as follows,
{
"json_class": "Item",
"v": [
1,
"chair"
]
}
But I'm not sure how to do that to make JSON for my elements as stated above. Google search didn't give me a proper way to do this.
Can anyone help me here?
it means this?:
require 'json'
my_arr= ["1st Value","2nd Value","3rd Value","4th Value"]
tmp_str= {}
tmp_str["desc"] = {}
my_arr.each do |x|
tmp_str["desc"]["#{x[0]}"] = x
end
puts JSON.generate(tmp_str)
you can iterate the string array ,then take the strings to hash object.JSON can easy to parse Hash objcect .
I have a perl code which read csv file. It contains grid data which needs to be updated at the front end.
First, here is the perl code which reads data and formats it so that the data can be pushed to front end for display.
my #array;
for my $column ($csv->column_headers) {
my $json = encode_json([ map { $_->{$column} } #$data ]);
push(#array, "$json;");
}
The final data is the #array which is passed to front end javascript code. The contents of #array is as follows.
["1","2"]; ["dd","ddd"]; ["wow","cool"]; ["HOLD","HOLD"];
This data is actually 4 columns with column header names as Id, Name, Comment and type. All these data are bundled up together in #array and passed to Javascript.
var header=[];
header[0] = #array[0];
}
This code above displays the below output if I do a console.log(header[0]); It means it is displaying the first element of the array. but I want to display the first element's element.
["1", "2"]
whereas it should display below output.
["1"]
In short, I want to know how can I access array elements elements. I tried using below code but it didn't work. Can someone please suggest?
var header=[];
header[0] = #array[0][0];
I am ultimately trying to put this data in grid by using below code.
for (var i=0;i<row_cnt;i++){
var row={};
row["Id"]=Id[i];
row["Name"]=Name[i];
row["Comment"]=Comment[i];
row["type"]= type[i];
data[i]=row;
}
where Id[i] will corresponding to "1" in first loop and "2" in second loop. Similarly it will generate data for other columns. These are then assigned to rows and updated in grid.
As per matts suggestion, I edited the code like this
my $json = encode_json($data);
for my $column ($csv->column_names) {
push(#data_array, "var $column= $json;");
}
Now it displays below values at every cell of the grid.
[object Object]
Building on the answer to your previous question, I think you just need to swap out the loop at the end for this:
my $json = encode_json($data);
print "var data = $json;\n";
I have an Array[String] in scala like this
my_array: Array[String] = Array(RED;BLUE, RED;PINK, RED;ORANGE, RED;WHITE, RED;YELLOW,
RED;GREY,GREEN;BLUE, GREEN;PINK, GREEN;BROWN, GREEN;ORANGE, GREEN;WHITE, GREEN;YELLOW, GREEN;GREY)
and I need to get this result
my_new_array: Array[Array[String]] = Array(Array(RED;BLUE, RED;PINK, RED;ORANGE, RED;WHITE,RED;YELLOW, RED;GREY),
Array(GREEN;BLUE, GREEN;PINK, GREEN;BROWN, GREEN;ORANGE, GREEN;WHITE, GREEN;YELLOW, GREEN;GREY),
Array(RED;BLUE, GREEN;BLUE), Array(RED;PINK, GREEN;PINK),
Array(RED;ORANGE, GREEN;ORANGE), Array(RED;WHITE, GREEN;WHITE),
Array(RED;YELLOW, GREEN;YELLOW), Array(RED;GREY, GREEN;GREY))
These should be te steps
get a list of unique colors. this means I have to split by ";" each string
once I have this list I have to create a new Array contained the original strings grouped by each single color
Does anyone have an hint?
Provided I've understood your question correctly, this should work (probably not the most efficient solution ever)
myArray
.flatMap(_.split(';')) // get all the colors
.distinct // get the unique set of colors
.map(color => myArray.filter(_.contains(color))) // map each color to each group containing it
I'm using contains assuming that for "YELLOW" you want to match both "YELLOW";"RED" and "RED";"YELLOW".
In case you want to match only the former, you can use startsWith intead.