org.json.JSONObject converting number to exponential form - json

I am using org.json.JsonObject (20190722 version) to convert a string into a json object. It's converting the decimal numbers into exponential format.
String jsonString = "{\"value1\":12345678,\"value2\":\"Name\",\"value3\":123456789.345}";
JSONObject json = new JSONObject(jsonString);
I am expecting output:
"value2":"Name","value1":12345678,"value3":123456789.345
But I am getting:
"value2":"Name","value1":123456789,"value3":1.23456789345E8

Related

Unable to parse a json string Binary in spark scala

I have a Binary json array string which I need to parse in Spark scala.
This is what I am doing:
import com.github.wnameless.json.flattener.JsonFlattener
def extractBlob(jsonString: String,
defaultValue: String = ""): String = {
JsonFlattener
.flatten(jsonString)
.toString
}
val extractBlobAsText = udf((jS: String) => extractBlob(jS))
val df1 =
df.withColumn("extracted",extractBlobAsText(unhex(col("hex").cast(StringType))))
df1.show(false)
But I am getting this error
com.eclipsesource.json.ParseException: Unexpected end of input at 1:1 at
com.eclipsesource.json.JsonParser.error(JsonParser.java:490) at
com.eclipsesource.json.JsonParser.expected(JsonParser.java:484) at
com.eclipsesource.json.JsonParser.readValue(JsonParser.java:193) at
com.eclipsesource.json.JsonParser.parse(JsonParser.java:152) at
com.eclipsesource.json.JsonParser.parse(JsonParser.java:91) at
com.eclipsesource.json.Json.parse(Json.java:295) at
com.github.wnameless.json.flattener.JsonFlattener.<init>(JsonFlattener.java:144) at
com.github.wnameless.json.flattener.JsonFlattener.flatten(JsonFlattener.java:100) at
notebook0.Cell5$285.extractBlob(Cell5:12) at
Also, I do not know the JSON schema. All I know is that it is a Json array. So I also need to do explode later.

How to remove backslash from JSON string using object mapper

Here is the code of getting Json string from object using object mapper:
let jsonString = Mapper().toJSONString(freechargeRequest)
but the output is coming with backslash as follows:
"{\"region\":\"http:\/\/testrm.getquickride.com:8080\/dishaapiserver\/qr_freecharge_failure.do\",\"merchantId\":\"8mILp0KGOdEG57\",\"productInfo\":\"auth\",\"mobile\":\"1357924680\",\"channel\":\"IOS\",\"amount\":\"200\",\"surl\":\"http:\/\/testrm.getquickride.com:8080\/dishaapiserver\/qr_freecharge_success.do\",\"merchantTxnId\":\"510420180705115938\"}"
I wanted the output without backslashes in JSON string.

How to create a map from DataFrame and convert it to json string

I am trying to get a map of columnName->values from a dataframe.I tried
val g=dataFrame.limit(limit)
val p=g.columns.map(i=>(i,g.select(i).map(_.get(0)).collect()))
and
val g=dataFrame.limit(limit)
val p=g.columns.map(i=>(i->g.select(i).map(_.get(0)).collect()))
But bot gives me an Array[String,Array[Any]]
I want to get a map[String,Array[Any]]
I also tried .toMap at the end to convert array to map,
val g=dataFrame.limit(limit)
val p=g.columns.map(i=>(i,g.select(i).map(_.get(0)).collect())).toMap
val gson=new Gson
gson.toJson(p)
but this gives me json string of the form
{"key1":"eq_site_deductible","value1":[0.0,0.0,0.0,],"key2":"county","value2":["CLAY COUNTY","CLAY COUNTY","Mary county"]}
I want to get a json string of the form {"eq_site_deductible":[value array],"county":[value array]}
If you just need the json, you don't need to convert it into map.
Below snippet can be used to write the dataframe content into a json file
dataFrame.write.format("json").save("result.json")
Or if you need the json string to be processed further in your code, you can use dataframe.toJSON to get the RDD[String], in which String will be the json

How to Convert JsonValue to JSONObject

I m using Jaql to manipulate data(data stored in json converted to string). The output for Jaql will be a com.ibm.jaql.json.type.JsonValue. It can be stored into string through toString() method. I got to do some json manipulations on the returned value. Problem here is, the output is not actual simple JSONObject. Is there a way to transform my string or JsonValue to JSONObject???
Please help.
Just use the String constructor of JSONObject:
JSONObject json = new JSONObject(jsonString);

Receiving JSON in salesforce

I am trying to receive a JSON string in salesforce by converting a blob in the body of an Http request. However, when I convert the blob to a string there are \ characters that get inserted into the request which prevents me from parsing.
I then tried to take the string and remove all \ characters... that didn't work either.
RestRequest req = RestContext.request;
Blob jsonBlob = req.requestBody;
String jsonString = jsonBlob.toString();
return jsonString;
The original string (the one that is received as a blob) looks like this:
{"putTimeCard":{"timecard":{"timeCardID": "","employeeID": ""}}
And after converting to a salesforce string and assigned to the jsonString is altered to:
{\"putTimeCard\":{\"timecard\":{\"timeCardID\": \"\",\"employeeID\": \"\"}}
Has anyone found a solution for this?
Thanks
The JSON Deserializer can parse the string with the escape characters. You can either deserialize into an object like so:
String jsonString = '{\"putTimeCard\":{\"timecard\":{\"timeCardID\": \"\",\"employeeID\": \"\"}}}'
Timecard t = (Timecard) JSON.deserialize(jsonString, Type.forName('Timecard'));
or if you just want a map of objects you can do the following:
String jsonString = '{\"putTimeCard\":{\"timecard\":{\"timeCardID\": \"\",\"employeeID\": \"\"}}}'
Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(jsonString);