0

I'm working with the following JSON structures

{
    "-L6Tr0Wl5fuG3tDgUPCa": {
        "List": "{'x': [0.02245, 0.02196], 'y': [0.96941, 0.97014], 'z': [0.05344, 0.05368]}",
        "Index": "17361"
    },
    "-L6Tr4j05NV6BJKcaRSe": {
        "List": "{'x': [0.03196, 0.01537], 'y': [0.96795, 0.96966], 'z': [0.05051, 0.04929]}",
        "Index": "17362"
    }
}

The name of each entry is random (e.g. L6Tr0Wl5fuG3tDgUPCa) that is generated by firebase whenever we push a new entry. What is the best way to parse and iterate through each entry of such a JSON file in python?

The file is huge with a couple of thousands of such entries.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    did you search for `python parse json` on SO? did you do any other reasearch? – Patrick Artner Mar 04 '18 at 12:43
  • Yes, I found quite a lot of examples and they all assumed known tags to parse. For example: data["known_tag"][0]. – Mobashyr Mohammad Mar 04 '18 at 12:44
  • So you know how to get from a json string to a dict aready? whats the problem then? You can `for key in dictName:` and then use `dictName[key]` to access its content. There is also `.items()` and `.keys()` on the dict. What is the _exact_ problem you have? show some code, an error message, expected output and whats not correct with your approach. – Patrick Artner Mar 04 '18 at 13:01
  • Firebase has different structure and I am facing issues with same on python – Baljeetsingh Sucharia Apr 11 '19 at 04:49

1 Answers1

2

I've never done Python before, but this seems to work in https://www.python.org/shell/:

import json
data = {
    "-L6Tr0Wl5fuG3tDgUPCa": {
        "List": "{'x': [0.02245, 0.02196], 'y': [0.96941, 0.97014], 'z': [0.05344, 0.05368]}",
        "Index": "17361"
    },
    "-L6Tr4j05NV6BJKcaRSe": {
        "List": "{'x': [0.03196, 0.01537], 'y': [0.96795, 0.96966], 'z': [0.05051, 0.04929]}",
        "Index": "17362"
    }
}

for key in data:
  print(key, data[key])

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • if you see the JSON of firebase - we have less control on where [ bracket is available. the structure is as is and we want to deserialize - I have done it in C# but python am trying to crack. – Baljeetsingh Sucharia Apr 11 '19 at 04:51