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.

4
  • 1
    did you search for python parse json on SO? did you do any other reasearch? Commented Mar 4, 2018 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]. Commented Mar 4, 2018 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. Commented Mar 4, 2018 at 13:01
  • Firebase has different structure and I am facing issues with same on python Commented Apr 11, 2019 at 4:49

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.