JSON decoding in PERL - Maintaining the original data type -
i writing simple perl script read json file , insert mongodb. facing issues json decoding. non-string values in original json getting converted object type after decode_json
.
input json(only part of since it's original huge) -
{ "_id": 2006010100000801089, "show_image" : false, "event" : "publish", "publish_date" :1136091600, "data_version" : 1 }
json gets inserted mongodb -
{ "_id": numberlong("2006010100000801089"), "show_image" : bindata(0,"ma=="), "event" : "publish", "publish_date" :numberlong(1136091600), "data_version" : numberlong(1) }
i providing custom _id
documents, want converted numberlong
type. working expected can see json above. notice how other non-string values show_image
, publish_date
, data_version
got converted it's object representation.
there way can retain original type these values?
perl code snipper insert -
use mongodb; use mongodb::oid; use json; use json::xs while(my $record = <$source_file>) { $record_decoded = decode_json($record); $db_collection->insert($record_decoded); }
perl version used v5.18.2.
i looked json::xs docs couldn't find way this. appreciated. in advance!
i new perl. sorry if trivial question.
i providing custom _id documents, want converted numberlong type. working expected can see json above. notice how other non-string values show_image, publish_date , data_version got converted it's object representation.
from example of data types matching aside boolean value show_image
being converted binary data.
it expected numeric types displayed numberlong
or numberint
when queried mongo
shell. mongo
shell uses javascript, has single numeric type of number
(64-bit floating point). shell helpers numberlong()
, numberint()
used represent values in mongodb's bson data types not have native javascript equivalent.
referring sample json, want value of show_image inserted false instead of bindata(0,"ma==") , publish_date inserted 1136091600 instead of numberlong(1136091600)
while it's ok insert publish_date
unixtime if suits use case, may find more useful use mongodb's date
type instead. there convenience methods querying dates including date aggregation operators. fyi, date fields displayed in mongo
shell isodate()
wrapper.
the boolean value show_image
needs assist, though.
if use data::dumper
inspect result decode_json()
, see show_image
field blessed object:
'show_image' => bless( do{\(my $o = 0)}, 'json::pp::boolean' )
in order expected boolean
value in mongodb, recommended approach in mongodb module docs use boolean
module (see: mongodb::datatypes).
i couldn't find obvious built-in option json or json::xs support serialising booleans other json emulated boolean class, 1 solution use data::clean::base
module part of data::clean::json
distribution.
sample snippet (excluding mongodb set up):
use data::clean::base; use boolean; $cleanser = data::clean::base->new( 'json::xs::boolean' => ['call_func', 'boolean::boolean'], 'json::pp::boolean' => ['call_func', 'boolean::boolean'] ); while (my $record = <$source_file>) { $record_decoded = decode_json($record); $cleanser->clean_in_place($record_decoded); $db_collection->insert($record_decoded); }
sample record saved in mongodb 3.0.2:
{ "_id": numberlong("2006010100000801089"), "event": "publish", "data_version": numberlong("1"), "show_image": false, "publish_date": numberlong("1136091600") }
Comments
Post a Comment