I have a JSON like
{
"company_id": "Sahil",
"company_name": "Sahil",
"ats_operators": ["123"],
"ids": [
{
"duns_id": "1234"
}
],
"company_symbol": "1234"
}
I wanted to convert the above JSON into the Go Structure.
I have one way to do it like:
type AutoGenerated struct {
CompanyID string `json:"company_id"`
CompanyName string `json:"company_name"`
AtsOperators []string `json:"ats_operators"`
Ids []struct {
DubnsID string `json:"dubns_id"`
} `json:"ids"`
CompanySymbol string `json:"company_symbol"`
}
But i wanted to use the Go-Map instead of Nested structure.
I tried to use the below code but it is unable to parse the above JSON.
type Test struct {
CompanyID string `json:"company_id"`
CompanyName string `json:"company_name"`
CompanySymbol string `json:"company_symbol"`
IDs map[string][]string `json:"ids"`
AtsOperators []string `json:"ats_operators"`
}
Please help and let me know what is the wrong with the above Go structure?
Do something like this and try. If you are fetching the data from mongodb then keep bson:"" part else just json tags is ok.
type DubnsID struct {
DubnsId string `bson:"dubns_id" json:"dubns_id"`
}
type AutoGenerated struct {
CompanyID string `bson:"company_id" json:"company_id"`
CompanyName string `bson:"company_name" json:"company_name"`
AtsOperators []string `bson:"ats_operators" json:"ats_operators"`
Ids map[string][]DubnsID `bson:"ids" json:"ids"`
CompanySymbol string `bson:"company_symbol" json:"company_symbol"`
}
You might have to use a struct like this:
type AutoGenerated struct {
CompanyID string `json:"company_id"`
CompanyName string `json:"company_name"`
AtsOperators []string `json:"ats_operators"`
Ids []map[string]interface{} `json:"ids"`
CompanySymbol string `json:"company_symbol"`
}
Related
I have a collection like this;
{
"_id" : ObjectId("5d428c8b0edc602c155929c5"),
"source" : "connection",
"class" : "dns",
"ts" : 1503528301.909886,
"uid" : "C8avTr2cLyrJJxkN9",
}
If I print the key and type of key in mongo shell, I can see that ts actually is a string:
ts string
When I receive a query, I need to get the ts value from the URL and do a .find() query.
GET http://localhost:3300/alerts?ts=1503528332.909886&limit=100&page=1
startTs = req.query.ts
This may have to be converted to a ts without the '.' after the second. I have looked at converting 1503528332.909886 to float, multiply by 1000 1503528332909.886 and truncate to integer 1503528332909. I have also tried using both string and number format.
results.results = await model
.find({"ts": {"$gte": <ts_variable: what format do I use?> }})
.skip(startIndex)
.exec();
res.paginatedResults = results;
If I only use ".find({})" and not try to select based on ts, everything works as expected. Appreciate any tips you have.
One of my requirement in the javascript, I am trying to convert the string which is passing from the database to javascript object.
Step1:
String passing from the databse:
"validator":["required","numeric","maxLength:14","{type: amountValidate}"]
Step2: Converting to javascript object using JSON.Parse() method, output as follows:
validator: Array(4)
0: "required"
1: "numeric"
2: "maxLength:14"
3: "{type: amountValidate}"
length: 4
Expected output is:
In the below code amountValidate is converting into the function by tabulator js api.
validator:["required","numeric","maxLength:14",{
type:amountValidate,
}]
Since I am applying the below function to the type:amountValidate, it should behave as a variable and it should not be in the double quotes.
var amountValidate = function(cell, value, parameters){
var regex = /^\s*-?(\d+(\.\d{1,2})?|\.\d{1,2})\s*$/
var n = value.match(regex);
if(n !== null){
return true;
}else{
return false;
}
}
Thanks in advance.
The main problem here is that your string is not a valid JSON. Should be something like:
'{"validator": ["required","numeric","maxLength:14", {"type": "amountValidate"}]}'
There are multiple json formatters/validators online, like this one, that you could use to check it.
As I mentioned in THIS QUESTION, I have problem when getting the response from the server.
I receive an array of objects with these attributes:
[{
"Id": 143187001116603, // VERY big number which I want to convert it to string
"Name": "تملی612", // string
"Title": "تسهیلات مسکن بانک ملی-اسفند96", // string
"InsCode": "IRO6MELZ96C1" // string
},
...
]
Any simple way to convert a specified type (Number) to string in a JSON file?
I see the replacer argument in JSON.stringify() but I have no idea how to use that...
UPDATE
One of those Ids, is: 9481703061634967 but JS convert that to 9481703061634968!
UPDATE
As Jonas H said in this answer, JS run the default parsing and that's why i lose my real Id value!
Any idea?
Transform the response to string, then apply a repalce with a regex to convert Id field to string type:
const axios = require('axios');
axios.get(url, { transformResponse: [data => data] }).then(response => {
let parsed = JSON.parse(response.data.replace(/"Id":(\d+),/g, '"Id":"$1",'));
console.log(parsed);
});
Assuming that you receive the data as a Json string with the numbers inside them, there is no way to preserve the data using JSON.parse. Even if you use the second argument to add a transformation function, it will only be run after the default parsing has parsed the numbers with a loss of information in case of large numbers. You need to manipulate the string directly to wrap the number in quotes using e.g. a regular expression.
You can also use the json-bigint npm package: https://www.npmjs.com/package/json-bigint
you can use replacer in JSON.stringify() like :
var obj = {
"Id": 143187001116603, // VERY big number which I want to convert it to string
"Name": "تملی612", // string
"Title": "تسهیلات مسکن بانک ملی-اسفند96", // string
"InsCode": "IRO6MELZ96C1" // string
};
function replacer(name, val) {
// convert Number to string
if ( val && val.constructor === Number ) {
return val.toString();
} else {
return val; // return as is
}
};
JSON.stringify(obj, replacer, 4);
// result
{
"Id": "143187001116603",
"Name": "تملی612",
"Title": "تسهیلات مسکن بانک ملی-اسفند96",
"InsCode": "IRO6MELZ96C1"
}
function replacer(key, value) {
// Filtering out properties
if (key === 'Id') {
return value.toString();
}
return value;
}
const t = [{
"Id": 143187001116603, // VERY big number which I want to convert it to string
"Name": "تملی612", // string
"Title": "تسهیلات مسکن بانک ملی-اسفند96", // string
"InsCode": "IRO6MELZ96C1" // string
},
{
"Id": 9481703061634967, // VERY big number which I want to convert it to string
"Name": "تملی232", // string
"Title": "تسهیلات مسکن بانک ملی-اسفن216", // string
"InsCode": "IRO6MSDZ96C1" // string
}
]
const stringifiedValue = JSON.stringify(t, replacer)
console.log(JSON.parse(stringifiedValue))
Try this using replacer callback for JSON.stringify.
Feedbacks welcome.
I am following one strange issue. Following is the detailed description
My object of JSON String
public class ChartSearchCriteria
{
public ChartSearchCriteria()
{
}
public DateTime StartDate
{
get;
set;
}
public DateTime EndDate
{
get;
set;
}
public Int32 ClassType
{
get;
set;
}
public Int32 InstructorID
{
get;
set;
}
}
I am converting this object to JSON string and assigning to one hidden field
ChartSearchCriteria objChartSearchCriteria = new ChartSearchCriteria()
{
StartDate = startDate,
EndDate = endDate,
ClassType = Convert.ToInt32(ddlClassType.SelectedValue)
};
string jsonSearchCriteria = new JavaScriptSerializer().Serialize(objChartSearchCriteria);
// Here in jsonSearchCriteria i am getting following string
// "{\"StartDate\":\"\\/Date(1436466600000)\\/\",\"EndDate\":\"\\/Date(1439145000000)\\/\",\"ClassType\":0,\"InstructorID\":0}"
hdnSearchData.Value = jsonSearchCriteria;
I want to pass this json string to another page with query string. I have used following javascript to get url
alert(document.getElementById("hdnSearchData").value);
// Here i am getting following JSON string from hidden field
// {"StartDate":"\/Date(1436466600000)\/","EndDate":"\/Date(1439145000000)\/","ClassType":0,"InstructorID":0}
var searchData = JSON.parse(document.getElementById("hdnSearchData").value);
var redirectUrl = _spPageContextInfo.webAbsoluteUrl + "/_layouts/15/DetailedChart.aspx?searchdata=" + encodeURIComponen(JSON.stringify(searchData));
Now I have used following code to Deserialize that json string to object into another page where I have passed that json string as query string
string jsonString = Convert.ToString(Page.Request.QueryString["searchdata"]);
jsonString = HttpUtility.UrlDecode(jsonString);
// Here I am getting following json string
// {"StartDate":"\/Date(1436466600000)\/","EndDate":"\/Date(1439145000000)\/","ClassType":0,"InstructorID":0}
JavaScriptSerializer oJS = new JavaScriptSerializer();
ChartSearchCriteria oRootObject = new ChartSearchCriteria();
oRootObject = oJS.Deserialize<ChartSearchCriteria>(jsonString);
So here i am getting exception like:
"Date(234234000)" cannot be converted to date time when Deserializing json string to object
The only thing which I get is that while assigning to json string to hidden field, It is losing escape character from json.
JSON String created from server side :
{\"StartDate\":\"\/Date(1436466600000)\/\",\"EndDate\":\"\/Date(1439145000000)\/\",\"ClassType\":0,\"InstructorID\":0}"
JSON string gotten from client side using javascript:
{"StartDate":"/Date(1436466600000)/","EndDate":"/Date(1439145000000)/","ClassType":0,"InstructorID":0}
So you can see above both different string which shows while assigning json string to hidden field , it is removing escape characters and that's why I cannot convert it back to object into another page.
I am sure 100% that it is issue related to escape character because i have checked deserialize method with following string and it is working fine
{\"StartDate\":\"\/Date(1436466600000)\/\",\"EndDate\":\"\/Date(1439145000000)\/\",\"ClassType\":0,\"InstructorID\":0}"
So how can I resolve that issue? My final goal is to pass json string to another page and deserializing into same object.
Any help will be highly appreciated and let me know anyone want some more information on it.
I have resolved issue by using following code.
string jsonString = Convert.ToString(Page.Request.QueryString["searchdata"]);
jsonString = HttpUtility.UrlDecode(jsonString);
// Here I am getting following json string
// {"StartDate":"\/Date(1436466600000)\/","EndDate":"\/Date(1439145000000)\/","ClassType":0,"InstructorID":0}
// By using following line I have corrected json string and now it is being deserialized to object.
jsonString = jsonString.Replace("/", "\\/");
JavaScriptSerializer oJS = new JavaScriptSerializer();
ChartSearchCriteria oRootObject = new ChartSearchCriteria();
oRootObject = oJS.Deserialize<ChartSearchCriteria>(jsonString);
I want to convert ObjectID (Mongodb) to String in JavaScript.
When I get a Object form MongoDB. it like as a object has: timestamp, second, inc, machine.
I can't convert to string.
Try this:
objectId.str
See the doc.
ObjectId() has the following attribute and methods:
[...]
str - Returns the hexadecimal string representation of the object.
in the shell
ObjectId("507f191e810c19729de860ea").str
in js using the native driver for node
objectId.toHexString()
Here is a working example of converting the ObjectId in to a string
> a=db.dfgfdgdfg.findOne()
{ "_id" : ObjectId("518cbb1389da79d3a25453f9"), "d" : 1 }
> a['_id']
ObjectId("518cbb1389da79d3a25453f9")
> a['_id'].toString // This line shows you what the prototype does
function () {
return "ObjectId(" + tojson(this.str) + ")";
}
> a['_id'].str // Access the property directly
518cbb1389da79d3a25453f9
> a['_id'].toString()
ObjectId("518cbb1389da79d3a25453f9") // Shows the object syntax in string form
> ""+a['_id']
518cbb1389da79d3a25453f9 // Gives the hex string
Did try various other functions like toHexString() with no success.
You can use $toString aggregation introduced in mongodb version 4.0 which converts the ObjectId to string
db.collection.aggregate([
{ "$project": {
"_id": { "$toString": "$your_objectId_field" }
}}
])
Use toString:
var stringId = objectId.toString()
Works with the latest Node MongoDB Native driver (v3.0+):
http://mongodb.github.io/node-mongodb-native/3.0/
Acturally, you can try this:
> a['_id']
ObjectId("518cbb1389da79d3a25453f9")
> a['_id'] + ''
"518cbb1389da79d3a25453f9"
ObjectId object + String will convert to String object.
If someone use in Meteorjs, can try:
In server: ObjectId(507f191e810c19729de860ea)._str.
In template: {{ collectionItem._id._str }}.
Assuming the OP wants to get the hexadecimal string value of the ObjectId, using Mongo 2.2 or above, the valueOf() method returns the representation of the object as a hexadecimal string. This is also achieved with the str property.
The link on anubiskong's post gives all the details, the danger here is to use a technique which has changed from older versions e.g. toString().
In Javascript, String() make it easy
const id = String(ObjectID)
this works, You have mongodb object: ObjectId(507f191e810c19729de860ea),
to get string value of _id, you just say
ObjectId(507f191e810c19729de860ea).valueOf();
In Js do simply: _id.toString()
For example:
const myMongoDbObjId = ObjectID('someId');
const strId = myMongoDbObjId.toString();
console.log(typeof strId); // string
You can use string formatting.
const stringId = `${objectId}`;
toString() method gives you hex String which is kind of ascii code but in base 16 number system.
Converts the id into a 24 character hex string for printing
For example in this system:
"a" -> 61
"b" -> 62
"c" -> 63
So if you pass "abc..." to get objectId you will get "616263...".
As a result if you want to get readable string(char string) from objectId you have to convert it(hexCode to char).
To do this I wrote an utility function hexStringToCharString()
function hexStringToCharString(hexString) {
const hexCodeArray = [];
for (let i = 0; i < hexString.length - 1; i += 2) {
hexCodeArray.push(hexString.slice(i, i + 2));
}
const decimalCodeArray = hexCodeArray.map((hex) => parseInt(hex, 16));
return String.fromCharCode(...decimalCodeArray);
}
and there is usage of the function
import { ObjectId } from "mongodb";
const myId = "user-0000001"; // must contains 12 character for "mongodb": 4.3.0
const myObjectId = new ObjectId(myId); // create ObjectId from string
console.log(myObjectId.toString()); // hex string >> 757365722d30303030303031
console.log(myObjectId.toHexString()); // hex string >> 757365722d30303030303031
const convertedFromToHexString = hexStringToCharString(
myObjectId.toHexString(),
);
const convertedFromToString = hexStringToCharString(myObjectId.toString());
console.log(`convertedFromToHexString:`, convertedFromToHexString);
//convertedFromToHexString: user-0000001
console.log(`convertedFromToString:`, convertedFromToString);
//convertedFromToHexString: user-0000001
And there is also TypeScript version of hexStringToCharString() function
function hexStringToCharString(hexString: string): string {
const hexCodeArray: string[] = [];
for (let i = 0; i < hexString.length - 1; i += 2) {
hexCodeArray.push(hexString.slice(i, i + 2));
}
const decimalCodeArray: number[] = hexCodeArray.map((hex) =>
parseInt(hex, 16),
);
return String.fromCharCode(...decimalCodeArray);
}
Just use this : _id.$oid
And you get the ObjectId string. This come with the object.
Found this really funny but it worked for me:
db.my_collection.find({}).forEach((elm)=>{
let value = new String(elm.USERid);//gets the string version of the ObjectId which in turn changes the datatype to a string.
let result = value.split("(")[1].split(")")[0].replace(/^"(.*)"$/, '$1');//this removes the objectid completely and the quote
delete elm["USERid"]
elm.USERid = result
db.my_collection.save(elm)
})
On aggregation use $addFields
$addFields: {
convertedZipCode: { $toString: "$zipcode" }
}
Documentation of v4 (right now it's latest version) MongoDB NodeJS Driver says: Method toHexString() of ObjectId returns the ObjectId id as a 24 character hex string representation.
In Mongoose, you can use toString() method on ObjectId to get a 24-character hexadecimal string.
Mongoose documentation
Below three methods can be used to get the string version of id.
(Here newUser is an object containing the data to be stored in the mongodb document)
newUser.save((err, result) => {
if (err) console.log(err)
else {
console.log(result._id.toString()) //Output - 23f89k46546546453bf91
console.log(String(result._id)) //Output - 23f89k46546546453bf91
console.log(result._id+"") //Output - 23f89k46546546453bf91
}
});
Use this simple trick, your-object.$id
I am getting an array of mongo Ids, here is what I did.
jquery:
...
success: function (res) {
console.log('without json res',res);
//without json res {"success":true,"message":" Record updated.","content":[{"$id":"58f47254b06b24004338ffba"},{"$id":"58f47254b06b24004338ffbb"}],"dbResponse":"ok"}
var obj = $.parseJSON(res);
if(obj.content !==null){
$.each(obj.content, function(i,v){
console.log('Id==>', v.$id);
});
}
...
You could use String
String(a['_id'])
If you're using Mongoose along with MongoDB, it has a built-in method for getting the string value of the ObjectID. I used it successfully to do an if statement that used === to compare strings.
From the documentation:
Mongoose assigns each of your schemas an id virtual getter by default which returns the document's _id field cast to a string, or in the case of ObjectIds, its hexString. If you don't want an id getter added to your schema, you may disable it by passing this option at schema construction time.