How to Convert JsonValue to JSONObject - json

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

Related

How to convert Object to Jooq JSON

I have a String (jsonData) being mapped to json via Jackson Object mapper as below to JaxB.
var completeJson = objectMapper.readValue(jsonData, Data.class);
myDto.setFirstName(completeJson.getFirstName())
myDto.setLastName(completeJson.getLastName()))
.....
.....etc..etc
I'm able to map to the above strings just fine. However, I'm having problems mapping
to a jooq JSON object. I guess I will now have to convert jsonData to jooq JSON.
How would I do this?
JSON newJson = objectMapper.(best method to use);
myDto.setJsonSource(newJson)
Or maybe I have to create some sort of wrapper?
DTO configured by jooq
public myDto setJsonSource(JSON jsonSource) {
this.jsonSource = jsonSource;
return this;
}
The org.jooq.JSON type is just a wrapper around a string, so why not just use:
JSON json = JSON.json(objectMapper.writeValueAsString(object));

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

difference between json string and parsed json string

what is the difference between json string and parsed json string?
for eg in javascript suppose i have a string in the json format say [{},{}]
parsing this string will also produce the same thing.
So why do we need to parse?
It's just serialization/deserialization.
In Javscript code you normally work with the object, as that lets you easily get its properties, etc, while a JSON string doesn't do you much good.
var jsonobj = { "arr": [ 5, 2 ], "str": "foo" };
console.log(jsonobj.arr[1] + jsonobj.str);
// 2foo
var jsonstr = JSON.stringify(jsonobj);
// cannot do much with this
To send it to the server via an Ajax call, though, you need to serialize (stringify) it first. Likewise, you need to deserialize (parse) from a string into an object when receiving JSON back from the server.
Great question. The difference is transfer format.
JSON is only the 'Notation' of a JavaScript Object, it is not actually the JavaScript 'object-literal' itself. So as the data is received in JSON, it is just a string to be interpreted, evaluated, parsed, in order to become an actual JavaScript 'Object-Literal.
There is one physical difference between the two, and that is quotation marks. It makes sense, that JSON needs to be a string to be transferred. Here is how:
//A JavaScript Object-Literal
var anObj = { member: 'value'}
//A JSON representation of that object
var aJSON = { "member":"value" }
Hope that helps. All the best! Nash
I think a parsed json string should be the string data into the actual javascript objects and data arrays (or whichever language the json string contains)
The JSON object contains methods for parsing JSON and converting values to JSON.
It can't be called or constructed, and aside from its two method properties it has no interesting functionality of its own.
JSONParser parser = new JSONParser();
Object object = parser.parse(Message.toString());
JSONObject arObj = (JSONObject) object;

Newtonsoft Object → Get JSON string

I have an object that is created by Newtonsoft's JSON serializer. I need to get the JSON string that was used to create the object. How do I serialize the object into a simple JSON string?
Try this:
public string jsonOut()
{
// Returns JSON string.
return JsonConvert.SerializeObject(this);
}