Parsing json blob with Python
Hello,
I'm having trouble finding much on this on the web. Any help would be massively appreciated. I have the following json objects: Code:
{ I can do this with the first object with: Code:
route = "enable" Code:
if args.fqdn in member['members'][0]['fqdn'] is there a way to loop through all objects? thanks in advance |
yes, you replace that 0 with a loop, like:
Code:
for member in element['groups']: |
Quote:
thank you. do you happen to know how to update a json key that is nested? Code:
parsed['enabled'] = True But it is added at the wrong level. Code:
parsed(member['enabled']) = True this is my last hurdle |
Quote:
Code:
member['members'][0]['fqdn'] = new_value |
Quote:
Code:
parsed = json.loads(json_data) Code:
member['members'][0]['fqdn'] = new_value KeyError: 'members' messed about with a few variations of that with no joy. |
Quote:
This is equally evident from merely reading the JSON snippet in post #1. |
Just grab the JSON, decode it, and print the resulting structure.
The correct way to handle JSON is always to first decode it, then manipulate the resulting data structure, then encode it. Treat the JSON conversion process as a "black box." Python, like every other language these days, uses "known good" software to do this. Do not attempt to manipulate the JSON data textually. |
Quote:
Hence why I am asking the question... |
Quote:
|
Quote:
|
Quote:
|
Well, first, JSON in your code sample isn't valid. You can see that by pasting it into https://jsonlint.com/ ...
Also, I'm not sure if you know how to iterate through a dictionary. Looping through it in a "for in" loop will get you its keys. Usually you want to call its .keys() (remember, explicit is better than implicit), .values() or .items() method and loop through that, depending on whether you need its keys, values, or both. |
As a side remark I want to add that in Linux-speak "blob" means an opaque piece of data (a file), something you cannot (easily) read, e.g. a binary closed source blob, some firmware etc.
JSON is not that. It is a simple text format, you can look at it in a text editor etc. There's no need to "decode" it, just format it nicely. What python does, it loads it into a structure that makes it easier to work with. There are other (command line) tools that can do similar things, jq comes to mind. |
Quote:
Quote:
Code:
for member in element['groups']: Quote:
Quote:
Quote:
|
@ondoho: What I am specifically saying is that you should treat the JSON data "as a blob." Don't try to "format it" – it doesn't need to be pretty – and don't try to write it yourself. Every programming language has encode/decode support for JSON (and other formats), and uses a standard set of binaries to do it. In this way you know that you can do what needs be done – to interchange structured data reliably with another client. You're handed a "blob" and the means to turn it into a data structure; then, to turn a data structure back into another "blob." And, if you do it this way, you can be confident that it will work correctly.
|
All times are GMT -5. The time now is 02:19 PM. |