1

I want to convert a csv to multilevel json. All I want is from:

brand,  active, prod   , prod_id, part, part_id, part_prise
canon,  1     , camera , 132    , body,  543   , 399
canon,  0     , camera , 132    , lens,  543   , 399
canon,  1     , printer, 132    , cart,  543   , 399
canon,  1     , printer, 872    , ink ,  543   , 399
sony ,  1     , camera , 132    , body,  543   , 399
sony ,  1     , lcd    , 112    , body,  543   , 399
sony ,  0     , lcd    , 132    , body,  543   , 399
sony ,  1     , laptop , 162    , body,  543   , 399

And following should be its JSON file.

brand : "canon",
active : 1,
prod : [{
     prod : "camera",
     prod_id : 132,
     part : [{
            part : "body",
            part_id : 543,
            part_prise:399
            },
           {
            part : "lens",
            part_id : 543,
            part_prise:399
            }]
         },{
     prod : "printer",
     prod_id : 132,
     part : [{
            part : "cart",
            part_id : 543,
            part_prise:399
            },
           {
            part : "ink",
            part_id : 543,
            part_prise:399
            }]
         }
      ]
...
...
...and so on..
halfer
  • 19,824
  • 17
  • 99
  • 186
MFarooqi
  • 1,004
  • 5
  • 12
  • 26
  • can you show the php or javascript that you are using to convert it ? – Jorge Y. C. Rodriguez Mar 12 '14 at 21:20
  • I'm looking for solution.. realy i don't have any file.. whatever i start.. i just quit.. simultaniously my brain start thinking to quickly and so many thing.. and become de-tracked.. – MFarooqi Mar 12 '14 at 21:33
  • Is just a bit confusing since you said *trying to resolve this for about a week*, and there is like 100 questions here in how to do it in javascript or php.. – Jorge Y. C. Rodriguez Mar 12 '14 at 21:37
  • but i'm looking in javascript.. the client want to run this all locallay..also can you please suggest me any in php...? – MFarooqi Mar 12 '14 at 21:38
  • your tag says JavaScript.. something like this could do with some modifications ... good luck http://stackoverflow.com/questions/17096509/php-convert-csv-to-specific-json-format – Jorge Y. C. Rodriguez Mar 12 '14 at 21:40
  • 1
    thank you for the tip. let me play with it.. i'll update here in either case .. – MFarooqi Mar 12 '14 at 21:45
  • no.. i couldn't get anything from it. here is the code i've created so far. but i want them to stay interlinked.. as i illustrator above.. – MFarooqi Mar 12 '14 at 22:02
  • http://jsfiddle.net/mfarooqi/4Nsqd/1/ – MFarooqi Mar 12 '14 at 22:19

1 Answers1

1

use csv dictreader, it prescent in the python csv package