1

I have a complex Json file made up of several nested objects and array of objects. The first object is an "OptionChain" that has an object called "Result". "Result" has nested objects: "Quote" and "Options". Finally, "Options" has nested array of objects named "Call" and "Put".

I have all the class variables annotated with @JSonProperty and using Spring Boot with Jackson to deal with the Object Mapping. I am new with using Jackson and Object Mapping.

When I run the program, I get this error:

Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "optionChain" (class com.thompson.OptionsImpliedMovement.data.OptionChain), not marked as ignorable (one known property: "result"])
 at [Source: (String)"{"optionChain":{"result":[{"underlyingSymbol":"KO","expirationDates":[1550188800,1550793600,1551398400,1552003200,1552608000,1553212800,1553817600,1555545600,1558051200,1561075200,1565913600,1579219200,1610668800],"strikes":[37.0,38.0,40.5,41.5,42.5,43.5,44.5,45.5,46.5,47.5,48.5,49.5,50.5,51.0,51.5,52.0,53.0,53.5,54.0],"hasMiniOptions":false,"quote":{"language":"en-US","region":"US","quoteType":"EQUITY","quoteSourceName":"Nasdaq Real Time Price","currency":"USD","exchangeDataDelayedBy":0,"earnin"[truncated 10817 chars]; line: 1, column: 17] (through reference chain: com.thompson.OptionsImpliedMovement.data.OptionChain["optionChain"])

Here is the main class, pom.xml, and two of my java classes:

Main:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.thompson.OptionsImpliedMovement.data.OptionChain;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;

@SpringBootApplication
public class OptionsImpliedMovementApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(OptionsImpliedMovementApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        String resourceURL = "https://query2.finance.yahoo.com/v7/finance/options/ko";
        HttpEntity<String> entity = new HttpEntity<>(headers);
        ResponseEntity<String> response = restTemplate.exchange(resourceURL, HttpMethod.GET,entity, String.class);

        String rawJson = response.getBody();

        ObjectMapper objectMapper = new ObjectMapper();

        OptionChain optionChain = objectMapper.readValue(rawJson, OptionChain.class);

    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.thompson</groupId>
    <artifactId>OptionsImpliedMovement</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>OptionsImpliedMovement</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

OptionChain:

import com.fasterxml.jackson.annotation.JsonProperty;

public class OptionChain {
    @JsonProperty("result")
    public Result result;

}    

Result:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty; 

public class Result  {

    @JsonProperty("underlyingSymbol")
    public String symbol;
    @JsonProperty("expirationDates")
    public long[] expirationDates;
    @JsonProperty("strikes")
    public double[] strikes;
    @JsonProperty("hasMiniOptions")
    public boolean hasMiniOptions;
    @JsonProperty("quote")
    public Quote quote;
    @JsonProperty("options")
    public Options option;
}

Here is a screenshot of hierarchy of Json file: Hierarchy of Json

And finally here is the full Json file attached: Full Json File

Thanks in advance for any help!

1 Answer 1

2

It seems to me that you're unmarshaling JSON that is { "optionChain": {...} } but you're doing so directly into an OptionChain. Instead, you need to define a class that has a single OptionChain member, because you are unmarshaling the outer object that contains this optionChain field (The { } that surrounds the entire response is the object you're trying to unmarshal).

So, for example:

public class OptionChainResponse {

   @JsonProperty("optionChain")
   private OptionChain optionChain;

   // getter/setter
}

And then:

OptionChainResponse optionChainResponse = objectMapper.readValue(rawJson, OptionChainResponse.class);
// do some validation or checking maybe
OptionChain optionChain = optionChainResponse.getOptionChain();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.