1

I'm studying Spring Boot and I know how to read JSON file from resources directory but I want to get particular data, not whole data. like localhost:8080/user returns user name.

below is my current code

package com.example;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.*;



@RestController
@EnableAutoConfiguration
public class HelloWorld {

    @Value("classpath:json/test.json")
    private Resource resourceFile;
    @RequestMapping("/")
    Resource home() {
        return resourceFile;
    }


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

I want to read particular data in the test.json file. please give me some advice or steps. Thanks

JESS YANG
  • 25
  • 1
  • 2
  • 5
  • When you already read json from the file, just convert it to jsonNode or your pojo object and get any property you want. https://www.baeldung.com/jackson-json-to-jsonnode – Huy Nguyen Oct 22 '18 at 14:08

2 Answers2

3

As always, there are several possible approaches.

Besides a manual parsing and extracting approach (How do I load a resource and use its contents as a string in Spring) you can also try a more advanced approach and use a library like jackson-databind (https://github.com/FasterXML/jackson-databind).

Assuming this json object in your resource:

{
  "foo" : {
    "bar" : 42
  }
}

and the Jackson ObjectMapper already Injected:

@Autowired
private ObjectMapper objectMapper;

Option 1: Use a generic approach with JsonNode

    @Autowired
    ObjectMapper objectMapper;

    @RequestMapping("/")
    JsonNode home() throws IOException {
        JsonNode jsonNode = objectMapper.readTree(resourceFile.getFile());
        return jsonNode.get("foo").get("bar");
    }

Option 2: https://github.com/FasterXML/jackson-databind#1-minute-tutorial-pojos-to-json-and-back

ibexit
  • 3,465
  • 1
  • 11
  • 25
0

This is just a variation of the @ibexit answer with some suggestions.

  1. Use Option 2 in the ibexit answer (use a pojo) unless you really need a JsonNode (The likelyhood that you need a JsonNode rounds to 0% likely).
  2. Create a POJO to represent your Json as an object. See below for an example.
  3. I recommend that you use the @JsonIgnoreProperties(ignoreUnknown = true) annotation. Do a google search.
  4. In your example, it is not required to use the @JsonProperty annotation, but I like to use it.

There are other ways to set the "ignoreUnknown" value, the Jackson documentation is a good and valuable read.

Example POJOs

@JsonIgnoreProperties(ignoreUnknown = true)
public class TopClass
{
    @JsonProperty("foo") // This is optional in your example.
    private Foo foo;
}


@JsonIgnoreProperties(ignoreUnknown = true)
public class Foo
{
    @JsonProperty("bar")
    private int bar;
}

Example Code to read the POJO

private TopClass topClassVariableName;

topClassVariableName = objectMapper.readValue(JSON HERE, TopClass.class);
DwB
  • 37,124
  • 11
  • 56
  • 82