0

I am working on a JSON data of shape as below.Its a list of documents and i am showing the first one

info=[
   {id: 34,
    log:[
         {ttp : 80,
          segm :[
                 {
                  dr: '2 hours'
                  room: 'F32'
                  },
                 {
                   dr: '48 hours',
                   room: 'G45'
                  }
                 ],
           },
           {
            ttp : 45,
            segm:[
                  {
                  dr: '4 hours'
                  room: 'F32'
                  },
                 {
                   dr: '8 hours',
                   room: 'G45'
                  }
             ]
            }
           ]
        },
        {document},
        {document}
 ]

I have created this very simple document just for understanding of data structure.

Now the problem is i want to access dr and room which are in segm. To access them i am using three nested loops first loop to iterate over info. Second loop to iterate over log and third loop on segm.

for loop -> info -> for loop-> log -> for loop -> segm

But i am compromising on performance here as the data is growing it's becoming slower. I need a better solution (an optimized solution) for this that does not depend upon the data size. Also If we can avoid loops that would be better i think.

jarry jafery
  • 1,018
  • 1
  • 14
  • 25
  • I think from a plain performance point of view there unfortunately is better way, except when you have the json as a text file and want to save the time it takes to convert them to python dicts, then it would be possible at least under some assumptions (e.g. like if dr and room always are interesting, no matter on which level and in which document they occur etc). – jottbe Jul 04 '19 at 13:52
  • yes dr and room are always of interest but they will always at the same level as shown above. How we can use string to get them all. – jarry jafery Jul 04 '19 at 15:26
  • You can´t avoid the loops if all infos have logs and all logs have segm. If that does not happen, you should make a separate file with only those. A database would probably be more suitable in the long road. If you do need to read all, a faster solution would be [this](https://stackoverflow.com/questions/40588852/pandas-read-nested-json) – juvian Jul 04 '19 at 17:20
  • @juvian thanks for your solution but for my problem its no working. Any other idea? – jarry jafery Jul 05 '19 at 14:02
  • @jarryjafery not working probably means not using it correctly – juvian Jul 05 '19 at 18:01
  • @juvian it means its not getting into 3rd level of nesting where i want it. So i am manually processing it by the above method i am able to avoid one loop but it increases the post processing. So its taking more time. – jarry jafery Jul 09 '19 at 09:38
  • 1
    @jarryjafery https://repl.it/repls/RealisticSoupyTruespace – juvian Jul 09 '19 at 17:58
  • It worked @juvian – jarry jafery Jul 10 '19 at 15:04

0 Answers0