ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What I am trying to do is loop through all "members" objects (there could be 2,3 or 4) and updated the "enabled" key to "True" if for example the fqdn = "alb2.com."
I can do this with the first object with:
Code:
route = "enable"
fqdn = "alb1"
for member in element['groups']:
if args.fqdn in member['members'][0]['fqdn']:
if args.route == "enable":
parsed['enabled'] = True
I think my problem is this line:
Code:
if args.fqdn in member['members'][0]['fqdn']
-- explicitly the [0].
is there a way to loop through all objects?
thanks in advance
Last edited by flukeyLinux; 07-01-2022 at 08:35 AM.
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.
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.
This information is good. I am new enough to python and have not come across decoding the json first. I must look that up. I am more of a bash head and this would be very easy there. But I need to use python as its a lambda.
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.
What I am trying to do is loop through all "members" objects (there could be 2,3 or 4) and updated the "enabled" key to "True" if for example the fqdn = "alb2.com."
This is slightly confusing because it's already "true" in your example input?
Quote:
Originally Posted by flukeyLinux
parsed is basically the json data:
Code:
parsed = json.loads(json_data)
Assuming element is somehow a subpart of parsed, I'm guessing you want
Code:
for member in element['groups']:
for data in member['members']:
if args.fqdn in data['fqdn']: # should this be "=" instead of "in"?
data['enabled'] = True
Quote:
Originally Posted by flukeyLinux
I am new enough to python and have not come across decoding the json first.
Your parsed = json.loads(json_data) step is decoding.
Quote:
Originally Posted by ondoho
As a side remark I want to add that in Linux-speak "blob" means an opaque piece of data (a file)[...]
There's no need to "decode" it, just format it nicely.
I think sundialscvs is just using non-standard terminology, as usual (in this case, "decode" = "parse", "blob" = "unparsed data").
Quote:
Originally Posted by dugan
Also, I'm not sure if you know how to iterate through a dictionary.
They're iterating over the arrays, not the dictionaries, I think? (I'm assuming that the JSON array gets parsed into a Python array.)
@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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.