hashmap - Scala - reformatting hash-map -
i have python code processes complex hashmap (example input), restructures , creates (example output) simplified version of it. i'm looking best way tackle scala, i.e. built in or external libs? i'm new scala , getting grips it, guidance here helpful.
this easy using python , i'm hoping scala.
input:
data_in = { 'map': { 'stats': { 'uphosts': u'3', 'timestr': u'thu mar 20 18:18:09 2014', 'downhosts': u'0', 'totalhosts': u'3', 'elapsed': u'1.71' }, 'scaninfo': { u'tcp': { 'services': u'80,443', 'method': u'syn' } }, 'command_line': u'command goes here' }, 'scan': { u'2a00:2384:0:208f::15': { 'status': { 'state': u'up', 'reason': u'nd-response' }, 'hostname': u'static.xyz.com', 'vendor': { u'00:0c:67:99:6f:96': u'vmware' }, 'addresses': { u'mac': u'00:gf:29:99:6d:96', u'ipv6': u'a848:2384:0:3456::15' }, u'tcp': { 80: { 'product': '', 'state': u'open', 'version': '', 'name': u'http', 'conf': u'3', 'extrainfo': '', 'reason': u'syn-ack', 'cpe': '' }, 443: { 'product': '', 'state': u'open', 'version': '', 'name': u'https', 'conf': u'3', 'script': { u'ssl-cert': u'place holder' }, 'extrainfo': '', 'reason': u'syn-ack', 'cpe': '' } } }, u'2a00:2384:0:208f::16': { 'status': { 'state': u'up', 'reason': u'nd-response' }, 'hostname': u'static.edf.com', 'vendor': { u'00:0c:55:ae:33:ff': u'vmware' }, 'addresses': { u'mac': u'00:54:29:fg:55:0f', u'ipv6': u'8938:8584:0:8685::16' }, u'tcp': { 80: { 'product': '', 'state': u'open', 'version': '', 'name': u'http', 'conf': u'3', 'extrainfo': '', 'reason': u'syn-ack', 'cpe': '' }, 443: { 'product': '', 'state': u'open', 'version': '', 'name': u'https', 'conf': u'3', 'script': { u'ssl-cert': u'place holder' }, 'extrainfo': '', 'reason': u'syn-ack', 'cpe': '' } } } } }
required output:
data_out_1 = [ {'address': u'2a00:2384:0:208f::15', 'hostname': u'static.xyz.com', 'ports': {80: {'reason': u'syn-ack', 'state': u'open'}, 443: {'reason': u'syn-ack', 'ssl_cert': u'place holder', 'state': u'open'}}}, {'address': u'2a00:2384:0:208f::16', 'hostname': u'static.edf.com', 'ports': {80: {'reason': u'syn-ack', 'state': u'open'}, 443: {'reason': u'syn-ack', 'ssl_cert': u'place holder', 'state': u'open'}}}]
that isn't typesafe.
hashmaps can't store different types of data*. start creating datastructures hold input data (case classes here).
so stats object might
case class stats(uphosts: int, timestr: datetime, downhosts: int, totalhosts: int, elapsed:double)
- ignoring subtyping here moment.
Comments
Post a Comment