3

I am getting the data from the UI as follows, all the below input data are strings.

cust_id : temp001 cust_chart_id : testing default_chart : false chart_pref_json : {range:6m,chart_type:candlestick,indicators:{},period:Daily,futurepadding:20} }

I am trying to store chart_pref_json in mongodb. This chart_pref_json object is actually stored in db as a string below,

{ "_id" : ObjectId("50aca4caf5d0b0e4d31ef239"), "cust_id" : "temp001", "cust_chart_id" : "testing", "default_chart" : "false", "created_at" : NumberLong("1353491658551"), **"chart_pref_json" : "{range:6m,chart_type:candlestick,indicators:{},period:Daily,futurepadding:20}" }**

But i actually want this chart_pref_json to be stored as an json object as below.

{ "_id" : ObjectId("50aca4caf5d0b0e4d31ef239"), "cust_id" : "temp001", "cust_chart_id" : "testing", "default_chart" : "false", "created_at" : NumberLong("1353491658551"), **"chart_pref_json" : {range:6m,chart_type:candlestick,indicators:{},period:Daily,futurepadding:20} }**

Can any one help me out on this issue.

2
  • when i am trying to save "chart_pref_json" : {range:6m,chart_type:candlestick,indicators:{},period:Daily,futurepadding:20} } in mongodb , it is saved as string "{range:6m,chart_type:candlestick,indicators:{},period:Daily,futurepadding:20}" instead i want to store as json {range:6m,chart_type:candlestick,indicators:{},period:Daily,futurepadding:20} without "". Commented Nov 22, 2012 at 10:19
  • Wanted and unwanted results look identical. Commented May 29, 2021 at 13:41

3 Answers 3

2

When you have the JSON code as a string, you first have to parse the JSON code and then convert the resulting JSON object to a BSON object.

You can use the class com.mongodb.util.JSON which comes with MongoDB. Here is a tutorial.

Sign up to request clarification or add additional context in comments.

Comments

2

Since this is a Java question, here's how you insert a JSON by using the MongoDB Java driver:

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

public class Main {
    public static void main(String[] args) {
        MongoClientURI uri = new MongoClientURI("mongodb://myuser:mypsw@localhost:27017/mydb");
        MongoClient client = new MongoClient(uri);
        MongoDatabase db = client.getDatabase("mydb");
        String json =
                "{\n" +
                "    \"_id\" : \"MyId\",\n" +
                "    \"foo\" : \"bar\"\n" +
                "}";
        db.getCollection("mycoll").insertOne(Document.parse(json));
    }
}

Comments

0

Before you set it as a field in your application, you need to use your language's JSON decoding abilities to encode it as an object. In PHP, you would do that like:

$db->collection->insert( array(
    'cust_id' => 'temp001',
    … your other fields …
    'chart_pref' => json_decode( $varContainingJson )
) );

For Java, the following example will help:

BasicDBObject obj = JSON.parse( varContainingJson ); 

Which is described at https://groups.google.com/forum/?fromgroups=#!topic/mongodb-user/Y3KyG_ZSfJg

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.