-1

I've already checked here and here.

I have the following json which has no root element and I want to loop through each item in objects and print the value for object_code:

{
  "count": 3,
  "objects": [
    {
      "object_code": "HN1059"
    },
    {
      "object_code": "HN1060"
    },
    {
      "object_code": "VO1013"
    }
  ]
}

I tried:

json='{"count": 3,"objects": [{"object_code": "HN1059"},{"object_code": "HN1060"},{"object_code": "VO1013"}]}'

for obj in json['objects']:
    print(obj.get('object_code'))


for obj in json[0]['objects']:
    print(obj.get('object_code'))

Neither work and I get the error:

TypeError: string indices must be integers

UPDATE 1

The suggested solutions don't work for me, maybe that's because I'm using it in the context of a Scrapy class, here's the full code that throws error

TypeError: 'NoneType' object is not iterable

import json
import scrapy

class MySpider(scrapy.Spider):
    name = 'mytest'
    start_urls = []

    def start_requests(self):
        s='{"count": 3,"objects": [{"object_code": "HN1059"},{"object_code": "HN1060"},{"object_code": "VO1013"}]}'
        obj = json.loads(s)
        for o in obj['objects']:
            print(o.get('object_code'))
6
  • your json contains JSON content : so a string, not a python structure Commented Dec 21, 2022 at 20:25
  • Your JSON is a string (thank you for correctly using the word JSON!). But why do you think you can manipulate that serialized data like unserialized data? Commented Dec 21, 2022 at 20:29
  • You could post a new question about your update. Include Scrapy in the tags if it's required to reproduce the issue. See minimal reproducible example. Commented Dec 21, 2022 at 21:38
  • @wjandrea I'll do that. As a best practice, should I delete this question since it's a duplicate anyway? Or would that not be fair to folks who already provided answers? Commented Dec 22, 2022 at 7:20
  • 1
    @wjandrea just tried to delete, I indeed can't "You cannot delete this question as others have invested time and effort into answering it." Commented Dec 23, 2022 at 9:56

2 Answers 2

2

You need to load fom the JSON content to python structure

import json

value = '{"count": 3,"objects": [{"object_code": "HN1059"},{"object_code": "HN1060"},{"object_code": "VO1013"}]}'
content = json.loads(value)

for obj in content['objects']:
    print(obj.get('object_code'))
Sign up to request clarification or add additional context in comments.

Comments

0

Use json.loads to convert the JSON string to a dict.

import json
s='{"count": 3,"objects": [{"object_code": "HN1059"},{"object_code": "HN1060"},{"object_code": "VO1013"}]}'
obj = json.loads(s)
for o in obj['objects']:
    print(o.get('object_code'))

6 Comments

This gives me error TypeError: 'NoneType' object is not iterable
@Adam It runs fine: ideone.com/v0Zx7H
@Adam Please provide a minimal reproducible example if there is a problem.
@Adam Maybe your problem is related to this: stackoverflow.com/questions/45784067/scrapy-on-a-json-response
I don't think so. That actually loads and tries to parse a Scrapy response object, but I'm just trying to parse a string, I never do anything with a response object...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.