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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.