title: Decode JSON with different structures with Swift
body: <p>I have json data with the same core structure from a nosql database (PK, SK, attributes).  The attributes part will be different depending on the value of SK.</p>
  <p>Example:</p>
  <pre><code>[
    {
      &quot;PK&quot;: &quot;POLL#1544693DF0E88EC-3225-410E-B156-D13781B238F6&quot;,
      &quot;SK&quot;: &quot;#METADATA#1544693DF0E88EC-3225-410E-B156-D13781B238F6&quot;,
      &quot;attributes&quot;: {
        &quot;latitude&quot;: &quot;53.34589121858683&quot;,
        &quot;longitude&quot;: &quot;-6.272215191675388&quot;,
        &quot;max_choices&quot;: 50,
        &quot;number&quot;: &quot;1544693&quot;,
        &quot;poll_open&quot;: false,
      }
    },
    {
      &quot;PK&quot;: &quot;POLL#1544693DF0E88EC-3225-410E-B156-D13781B238F6&quot;,
      &quot;SK&quot;: &quot;CHOICE#00a6ec5c-acc1-40f1-a087-31160d2cfc65&quot;,
      &quot;attributes&quot;: {
        &quot;distance&quot;: 790.95097525,
        &quot;latitude&quot;: 53.3416,
        &quot;price&quot;: &quot;€€&quot;,
        &quot;categories&quot;: [
          {
            &quot;title&quot;: &quot;Ramen&quot;,
            &quot;alias&quot;: &quot;ramen&quot;
          }
        ],
        &quot;vote_count&quot;: 0,
        &quot;longitude&quot;: -6.26274
      }
    }
  ]
  </code></pre>
  <p>Is it possible to use decode without errors?  I've been stuck on this for hours.</p>
  <p>I've defined the following:</p>
  <pre><code>struct Result: Codable {
      var PK: String
      var SK: String
      var attributes: String
  }
  </code></pre>
  <p>But, when I decode, I get the error:</p>
  <pre><code>typeMismatch(Swift.String, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: &quot;Index 0&quot;, intValue: 0), CodingKeys(stringValue: &quot;attributes&quot;, intValue: nil)], debugDescription: &quot;Expected to decode String but found a dictionary instead.&quot;, underlyingError: nil))
  </code></pre>
  <p>I just want to decode 'attributes' as a generic string and parse it later depending on the value of SK when I know how to properly handle it.  Why is this so difficult?</p>