skip to Main Content

I’m pretty new to json, I try to parse two elements (multiple occurences), I’m pretty sure I don’t iterate correctly.

My json file:

{
  "data": {
    "chapter": {
      "bibleId": "9f3cb709f9bded60-01",
      "bookId": "PSA",
      "id": "PSA.117",
      "content": [
        {
          "type": "paragraph",
          "style": "nb",
          "content": [
            {
              "type": "verse-number",
              "style": "v",
              "verseId": "PSA.117.1",
              "verseOrgId": [
                "PSA.117.1"
              ],
              "content": "1"
            },
            {
              "type": "verse-text",
              "verseId": "PSA.117.1",
              "verseOrgId": [
                "PSA.117.1"
              ],
              "verseText": "Sed ut perspiciatis unde omnis iste natus."
            },
            {
              "type": "verse-number",
              "style": "v",
              "verseId": "PSA.117.2",
              "verseOrgId": [
                "PSA.117.2"
              ],
              "content": "2"
            },
            {
              "type": "verse-text",
              "verseId": "PSA.117.2",
              "verseOrgId": [
                "PSA.117.2"
              ],
              "verseText": "Lorem ipsum dolor sit amet."
            }
          ]
        }
      ],
      "number": "117",
      "next": {
        "id": "PSA.118",
        "bookId": "PSA",
        "number": "118"
      },
      "previous": {
        "id": "PSA.116",
        "bookId": "PSA",
        "number": "116"
      },
      "copyright": "1969/77 Deutsche Bibelgesellschaft, Stuttgart",
      "verseCount": 2,
      "title": "Psalmi 117"
    },
    "studyContent": null,
    "chapterImage": null
  }
}

Desired output:

PSA.117.1       Sed ut perspiciatis unde omnis iste natus.
PSA.117.2       Lorem ipsum dolor sit amet.

My output:

PSA.117.1       Sed ut perspiciatis unde omnis iste natus.

My code:

import json
with open('data.json') as f:
    data = json.load(f)

for i, verse in enumerate(data['data']['chapter']['content']):
    if i == 0:
        print("%st%s" % (verse['content'][0]['verseId'], verse['content'][1]['verseText']))
    elif i == 1:
        print("%st%s" % (verse['content'][0]['verseId'], verse['content'][1]['verseText']))

3

Answers


  1. The issue in your script lies in the way you’re iterating through the JSON structure and accessing the verse data.

    The JSON structure you’ve provided has an array content within data[‘data’][‘chapter’][‘content’], which contains only one item (a paragraph), and then within that paragraph, it has an array content containing the verses.

    import json
    
    # Load the JSON data
    with open('data.json') as f:
        data = json.load(f)
    
    # Get the verses from the JSON structure
    verses = data['data']['chapter']['content'][0]['content']
    
    # Initialize variables for storing verse number and text
    verse_number = None
    verse_text = None
    
    # Iterate through the verse data
    for item in verses:
        if item['type'] == 'verse-number':
            verse_number = item['verseId']
            
        elif item['type'] == 'verse-text':
            verse_text = item['verseText']
            if verse_number is not None:
                print("%st%s" % (verse_number, verse_text))
                verse_number = None  # Reset verse number for the next verse
    
    Login or Signup to reply.
  2. You want one more loop over the inner content list, and filter the nodes which are of type verse-text:

    for chapter in data['data']['chapter']['content']:
        for verse in chapter['content']:
            if verse['type'] == 'verse-text':
                print(verse['verseId'], verse['verseText'], sep='t')
    
    Login or Signup to reply.
  3. a = data['data']['chapter']['content'][0]['content']
    for i, verse in enumerate(a):
        if i == 1:
            print("%st%s" % (verse['verseId'], verse['verseText']))
        elif i == 3:
            print("%st%s" % (verse['verseId'],verse['verseText'])) 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search