title: scrapy: Populate nested items with itemLoader
body: <p>I have this object I'm trying to populate with an itemLoader: </p>
  
  <pre><code>{
    "domains": "string",
    "date_insert": "2016-12-23T11:25:00.213Z",
    "title": "string",
    "url": "string",
    "body": "string",
    "date": "2016-12-23T11:25:00.213Z",
    "authors": [
      "string"
    ],
    "categories": [
      "string"
    ],
    "tags": [
      "string"
    ],
    "stats": {
      "views_count": 0,
      "comments_count": 0
    }
  }
  </code></pre>
  
  <p>Here's my items.py</p>
  
  <pre><code>class StatsItem(scrapy.Item):
      views_count=scrapy.Field()
      comments_count=scrapy.Field()
  
  class ArticleItem(scrapy.Item):
      domain = scrapy.Field()
      date_insert=scrapy.Field()
      date_update=scrapy.Field()
      date=scrapy.Field()
      title=scrapy.Field()
      url=scrapy.Field()
      body=scrapy.Field(
          output_processor=Join())
      date=scrapy.Field()
      authors=scrapy.Field(
          output_processor=Identity())
      categories=scrapy.Field(
          output_processor=Identity())
      tags=scrapy.Field()
      stats=scrapy.Field()
  </code></pre>
  
  <p>Part of my spider: </p>
  
  <pre><code>def parse(self, response):
      loader = ArticleItemLoader(response=response)
      parsed_uri = urlparse(response.url)
      domain = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri)
  
      loader.add_css('authors','span.meta-author')
      loader.add_css('title', 'h1.title-article')
      loader.add_value('url', response.url)
      loader.add_xpath('date_insert', '//div[@class=\'meta\']/time[@itemprop=\'datePublished\']/@datetime')
      loader.add_xpath('date_update', '//div[@class=\'meta\']/time[@itemprop=\'dateModified\']/@datetime')
      loader.add_value('domain', domain)
      loader.add_xpath('categories', '//ul[@class=\'breadcrumbs\']//li[not(contains(@class, \'home\'))]')
  </code></pre>
  
  <p>So far I have succesfuly populating every fields but "stats". I've checked this page <a href="https://stackoverflow.com/questions/25095233/correct-way-to-nest-item-data-in-scrapy">correct way to nest Item data in scrapy</a> but it seems to not be working anymore (I can't make it work, my error is TypeError: to_unicode must receive a bytes, str or unicode object, got StatsItem)</p>
  
  <p>I'd like to use the itemLoader but I dont see how I could populate my "stats" with my StatsItem</p>
  
  <p>Thx for the help</p>
  
  <p><strong>Edit</strong>
  I am close but it still doesnt work : </p>
  
  <pre><code>loader.add_value('stats', self.getStats(response))
  
  def getStats(self, response):
      statsLoader = StatsItemLoader(response=response)
      statsLoader.add_xpath('comments_count', '//div[@class=\'btn-count\']//a/text()')
      statsLoader.add_value('views_count', '42')
      return json.dumps(dict(statsLoader.load_item()))
  </code></pre>
  
  <p>but my output is like :
      {
         [...]
         "stats": "{\"comments_count\": \"0\", \"views_count\": \"42\"}"
      }</p>