14

I have a file that contains a HashMap customer list in json format.

Like this:

{"Davide":{"name":"Davide","cf":"FRCDVD","pi":"1234",
    "telephone":"333","website":"www","sector":"Student","address":"Rome"}}

This is just a one customer of list. Everytime the controller is called I want to take datas from the file and convert them into HashMap list.

I tried to do this with:

HashMap<String, Customer> listCustomer = new HashMap<>();
listCustomer = new ObjectMapper().readValue(pathCustomerFile, HashMap.class); //This line gives me error

I got this error:

org.codehaus.jackson.JsonParseException: Unexpected character ('/' (code 47)): maybe a (non-standard) comment? (not recognized as one since Feature 'ALLOW_COMMENTS' not enabled for parser)

How can I do that?

2
  • 1
    read this github.com/elastic/elasticsearch/issues/1394. maybe it will be useful
    – ema
    Commented Jun 12, 2015 at 10:25
  • 1
    May be pathCustomerFile is a String? I had a similar problem, I was wrong with the input parameter. Fixed by replacing with new File (pathCustomerFile)
    – Sergey K
    Commented Apr 27, 2021 at 12:48

6 Answers 6

28

I had this issue recently. I was trying to pass a path (in the form of a string) to readValue. You need to pass it either a string to parse, or a file object. Based on your variable nameI think you may have passed it the path to a file.

(Basically, it's reading the '/' in the file path and throwing errors on them.

2

In my case, this was exactly as @Sergey K pointed out in the earliest comments. So the credit goes to him.

String pathCustomerFile = "/blah/blah.json";  // <-- Problematic line
listCustomer = new ObjectMapper().readValue(pathCustomerFile, HashMap.class);

Turns out pathCustomerFile was String and jackson runtime was trying to interpret the value of this string as a JSON object, hence the error.

Unexpected character '/' is the very first character of the path value "/blah/blah.json"

You can also tell this by the checked exception that the readValue method throws, if it is ProcessingException, then chances are the parameter itself is going to be interpreted as the JSON string.

SOLUTION:

Wrapping the path string with File resolves the problem:

new ObjectMapper().readValue(new File(pathCustomerFile), HashMap.class);

You can verify this by the checked exception being thrown changing to IOException as well.

1

Please double check your input JSON string you don't have it wrongly escaped or / dangling somewhere. For example /\"name\". Then Provide the correct type mapping tis way:

new ObjectMapper().readValue(pathCustomerFile, new TypeReference<HashMap<String, Customer>>(){});

My answer was tested with jackson-mapper-asl 1.9.13.

** Your mapping just with HashMap.class will not give you desired results as Jackson will map your JSON into Map<String, Map>. You will find out, when you will try to get a value from Map and operate as if it were type of Customer.

0
1

I would create a POJO for customers having the list with the customers and the getter/setter pair, like that:

class CustomersFile{
  List<Customer> customers;

  //getter and setter
}

Then I would create class Customer with the field name and customerDetails, like that:

class Customer {
  String name;
  CustomerDetails details;

  //getters and setters for both fields
}

And finally I would create the class CustomerDetails with all the fields, like that:

class CustomerDetails {
  String name;
  String telephone;
  int pi; // and so on

  //getters and setters for all fields  
}

Then using object mapper I would map all the customers from json to my CustomersFile object:

ObjectMapper mapper = new ObjectMapper();
//this configuration is needed in case you have only one customer in your json.
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
CustomersFile customersFile = mapper.readValue(pathCustomerFile, CustomersFile.class);

To access the list of customers, just call:

List<Customer> customers = customersFile.getCustomers();

If you really need a HashMap then loop through the list and fill this hash map:

HashMap<String, Customer> map = new HashMap<>();
for(Customer customer : customers) {
  // as string you can use the id of the customer (pi) but its no necessary, just use your desired String
  map.put("String.valueOf(customer.getPi())", customer);
}

UPDATE

Below are the dependencies I use in my project's pom:

<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.4.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.4.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.1</version>
    </dependency>
8
  • mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); Error: no suitable method found for configure(DeserializationFeature,boolean)
    – Dave
    Commented Jun 12, 2015 at 10:39
  • what version of jackson do you use? Commented Jun 12, 2015 at 11:03
  • gson 2.3.1, jackson-mapper-asl 1.9.13
    – Dave
    Commented Jun 12, 2015 at 11:06
  • @DavideFruci see my UPDATE, also try it without configuring the mapper to accept the single value as array Commented Jun 12, 2015 at 13:08
  • I added your dependecies but I have the same error again.
    – Dave
    Commented Jun 13, 2015 at 18:46
1

I had the same issue. I was mistakenly sending comments in my request payload like '//'. Removing '//' worked for me.

0

I had the same error when I add comments in JSON. I find solution on https://github.com/elastic/elasticsearch/issues/1394

final ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
final HashMap<String, Customer> listCustomer = mapper.readValue(pathCustomerFile, HashMap.class); //This line gives me no error

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.