LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-14-2022, 05:54 AM   #1
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 282

Rep: Reputation: 30
Python nested JSON for loops


I have a JSON file that is structured like so
Code:
x.data[0].leaders[0].type
Where I need to loop through "data" and "leaders" to find every instance of "type". I have the following but it's telling me string indices must be integers. I thought I fixed that by putting the "[0]" by the arrays but it's still giving me the message. I feel like this is either a really simple fix or I completely screwed up.

Code:
x = json.loads(JSON)
for a in x["data"][0]:
        for b in a["leaders"][0]:
            print (b["type"])
 
Old 03-14-2022, 08:47 AM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,617

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555

Don't describe errors and data - provide the exact error message and a concise example that produces that error.

A common debugging technique in these sorts of situation is to make print progressively more general until you find where your assumptions fail - so doing "print(b)" and examine the output carefully. Also, often only some parts of the data are different - you can use try/except to determine that.

 
1 members found this post helpful.
Old 03-14-2022, 08:54 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,989

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
yes, you can insert a lot of debugging messages, like:
Code:
x = json.loads(JSON)
print(x)
print(x["data"])
print(x["data"][0])
for a in x["data"][0]:
    print(a)
    print(a["leaders"])
    print(a["leaders"][0])
    for b in a["leaders"][0]:
        print(b)
        print (b["type"])
and you will see which part is wrong and what should be modified.
"string indices must be integers" means you use strings as indices instead of integers.

As it was mentioned you can use try/except to catch errors (if you wish). And please post the exact error message, the real code and real json example (you can remove/replace sensitive info obviously)
 
Old 03-14-2022, 11:10 AM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,246

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
Post enough of the JSON to make your first code block work.
 
  


Reply

Tags
json, loops, python



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieve the content of a json file using shell script .Without jq /json Historia Linux - Newbie 12 03-25-2020 09:50 AM
LXer: JSON Lines: record-style JSON LXer Syndicated Linux News 0 01-28-2020 08:03 PM
Nested for loops and arrays in C iongrey Programming 10 04-24-2009 05:25 PM
Nested Loops In Shellscripting onacorpuscle Programming 1 11-09-2006 07:42 AM
Vectors not being reset in nested loops. C++ dangerboy Programming 8 09-22-2006 05:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration