Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

329
Views
Can't find a codec for class org.json.JSONArray
 private void getUsersWithin24Hours(String id, Map < String, Object > payload) throws JSONException {
  JSONObject json = new JSONObject(String.valueOf(payload.get("data")));
  Query query = new Query();

  query.addCriteria(Criteria.where("user_id").is(id).and("timezone").in(json.get("timezone")).and("gender").in(json.get("gender")).and("locale").in(json.get("language")).and("time").gt(getDate()));
  mongoTemplate.getCollection("user_log").distinct("user_id", query.getQueryObject());
 }

I was going to made a query and get result from mongodb and I was succeed with mongo terminal command:

db.getCollection('user_log').find({"user_id" : "1", "timezone" : {$in: [5,6]}, "gender" : {$in : ["male", "female"]}, "locale" : {$in : ["en_US"]}, "time" : {$gt : new ISODate("2017-01-26T16:57:52.354Z")}}) 

but from java when I was trying it gave me below error.

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class org.json.JSONArray

What is the ideal way to do this?

Hint : actually I think in my code error occurred of this part json.get("timezone"). because it contains array. When I am using hardcode string arrays this code works

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Here's an example of MongoDB from MongoDB University course with a MongoDB database named "students" with a collection named "grades" :

pom.xml

<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>

    <groupId>com.mongodb</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>test</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver</artifactId>
            <version>3.2.2</version>
        </dependency>
    </dependencies>
</project>

com/mongo/Main.java

package com.mongo;

import com.mongodb.MongoClient;
import com.mongodb.client.*;
import org.bson.Document;
import org.bson.conversions.Bson;

import javax.print.Doc;

public class Main {

    public static void main(String[] args) {

        MongoClient client = new MongoClient();
        MongoDatabase database = client.getDatabase("students");
        final MongoCollection<Document> collection = database.getCollection("grades");

        Bson sort = new Document("student_id", 1).append("score", 1);
        MongoCursor<Document> cursor = collection.find().sort(sort).iterator();

        try {
            Integer student_id = -1;
            while (cursor.hasNext()) {
                Document document = cursor.next();
                // Doing more stuff
            }
        } finally {
            cursor.close();
        }
    }
}
over 4 years ago · Santiago Trujillo Report

0

You don't have to use JSONObject/JSONArray for conversion.

Replace with below line if the payload.get("data") is Map

BasicDBObject json = new BasicDBObject(payload.get("data"));

Replace with below line if the payload.get("data") holds json string.

BasicDBObject json =(BasicDBObject) JSON.parse(payload.get("data"));
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!