title: Unit Testing JsonResult anonymous type in .Net Core
body: <p><strong>UPdated: Solution at bottom</strong></p>
  
  <p>In .net core JsonResult does not have a "Data" property.
  Many of the existing answers us that property to retrieve the needed values for unit testing.</p>
  
  <p>I can't seem to wrap my head around the problem here:</p>
  
  <pre><code>        public async Task&lt;IActionResult&gt; GetIndividualPerformanceMetrics(string npi)
      {
          var metrics = new List&lt;PhysicianMetric&gt;();
          var overallRank = new OverallPhysicianRank();
          var userNPI = User.GetSingleClaimValueByType(Constants.ClaimTypes.Npi);
          npi = string.IsNullOrWhiteSpace(npi) ? userNPI : npi;
          if (npi != null)
          {
  
              if (await _authorizationService.AuthorizeAsync(User, "IsMetricSupportTeam")
                  || User.GetAllClaimsByType(Constants.ClaimTypes.Npi).Any(n =&gt; n.Value == npi))
              {
                  metrics = (await _physicianMetrics.GetAllByNPIAsync(npi)).ToList();
              }
          }
          return Json(new { metrics });
      }
  </code></pre>
  
  <p>And in my unit test I'm doing the following:</p>
  
  <pre><code>            _physicianMetrics.Setup(pm =&gt; pm.GetAllByNPIAsync(validNPI))
              .ReturnsAsync(new List&lt;PhysicianMetric&gt;(){_physicianMetric});
  
          var anonymousDefinition = new { metrics = new List&lt;PhysicianMetric&gt;() };
  
          //Act
          var result = await (performanceMetricsController.GetIndividualPerformanceMetrics(validNPI)) as JsonResult;
          var obj = JsonConvert.DeserializeAnonymousType(result.Value.ToString(), anonymousDefinition);
          //Assert
          Assert.AreEqual(validNPI, obj.metrics[0].NPI);
  </code></pre>
  
  <p>The JsonResult is returning with the mocked repository values, but I can't seem to write the test so that I can ensure the JsonResult data is the data returned by the repository query. The deserialization fails with the following error:</p>
  
  <p>"Invalid character after parsing property name. Expected ':' but got: =. Path '', line 1, position 10. "</p>
  
  <p>I just need to validate the JsonResult values.</p>
  
  <p>Please Help.</p>
  
  <p><strong>Solution</strong>:  (Thanks Chris Pratt)</p>
  
  <p>I didn't realize that the JsonResult had not yet serialized the object, and that the JsonResult.Value was the pure anonymous type.  I just used reflection to call out the list of objects that I needed and was good to go:</p>
  
  <pre><code>        var result = await (performanceMetricsController.GetIndividualPerformanceMetrics(validNPI)) as JsonResult;
          var metrics = result.Value.GetType().GetProperty("metrics")?.GetValue(result.Value, null) as List&lt;PhysicianMetric&gt;;
  
          //Assert
          _physicianMetrics.Verify(pm =&gt; pm.GetAllByNPIAsync(validNPI), Times.Once);
          Assert.AreEqual(validNPI, metrics[0]?.NPI);
  </code></pre>