In flutter(dart), it is easy to deserialize Json and get a token from it, but when I try to serialize it again, the quotation marks around the keys and values with disappear.
String myJSON = '{"name":{"first":"foo","last":"bar"}, "age":31, "city":"New York"}';
var json = JSON.jsonDecode(myJSON); //_InternalLinkedHashMap
var nameJson = json['name']; //_InternalLinkedHashMap
String nameString = nameJson.toString();
Although the nameJson have all the double quotations, the nameString is
{first: foo, last: bar}
(true answer is {"first": "foo", "last": "bar"})
how to preserve Dart to remove the "s?
When encoding the object back into JSON, you're using .toString(), which does not convert an object to valid JSON. Using jsonEncode fixes the issue.
import 'dart:convert';
void main() {
String myJSON = '{"name":{"first":"foo","last":"bar"}, "age":31, "city":"New York"}';
var json = jsonDecode(myJSON);
var nameJson = json['name'];
String nameString = jsonEncode(nameJson); // jsonEncode != .toString()
print(nameString); // outputs {"first":"foo","last":"bar"}
}
Related
I'm getting a JSON from [1], due to the naming of the keys in the json I was unable to straightaway use either of the 2 approches in hadling JSON in ballerina:
Approach 1: Work with json values directly:
string s = check j.x.y.z;
Approach 2: Work with application-specific, user-defined subtype of anydata:
couldn't use cloneWithType or directly assign to a user defined record type
My workaround was:
type AllStockData record {
string open;
string high;
string low;
string close;
string volume;
};
AllStockData[] stockData = [];
public function main() returns #tainted error? {
http:Client httpClient = check new ("https://www.alphavantage.co");
json jsonPayload = check httpClient->get("/query?function=TIME_SERIES_INTRADAY&symbol="+searchSymbol.toUpperAscii()+"&interval=5min&apikey="+apiKey, targetType = json);
map<json> allData = <map<json>>jsonPayload;
foreach json item in <map<json>>allData["Time Series (5min)"] {
map<json> stockItem = <map<json>>item;
AllStockData stock = {
open: stockItem["1. open"].toString(),
high: stockItem["2. high"].toString(),
low: stockItem["3. low"].toString(),
close: stockItem["4. close"].toString(),
volume: stockItem["5. volume"].toString()
};
stockData.push(stock);
}
I was wondering If there is a better way to do this?
I prefer to work with application-specific types when dealing with JSON in Ballerina. You can use quoted identifiers in Ballerina to map the complete json payload into an application-specify type. I've used a query expression to filter out stack data entries into an array. There are other ways with slight variations to achieve the same thing.
Note that I've used Ballerina Swan Lake Alpha 3 to test this code.
import ballerina/io;
import ballerina/http;
type StockQuery record {|
MetaData 'Meta\ Data;
TimeSeries5min 'Time\ Series\ \(5min\);
|};
type MetaData record {|
string '1\.\ Information;
string '2\.\ Symbol;
string '3\.\ Last\ Refreshed;
string '4\.\ Interval;
string '5\.\ Output\ Size;
string '6\.\ Time\ Zone;
|};
type TimeSeries5min record {|
StockData...;
|};
type StockData record {|
string '1\.\ open;
string '2\.\ high;
string '3\.\ low;
string '4\.\ close;
string '5\.\ volume;
|};
public function main() returns error? {
http:Client httpClient = check new ("https://www.alphavantage.co");
json jsonPayload = check httpClient->get("/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey=demo",
targetType = json);
StockQuery stockQuery = check jsonPayload.cloneWithType(StockQuery);
TimeSeries5min timeSeries = stockQuery.Time\ Series\ \(5min\);
StockData[] stockDataArr = from var key in timeSeries.keys()
let StockData? stockData = timeSeries[key]
where stockData is StockData
select stockData;
io:println(stockDataArr);
}
I am practicing JSON parsing and I encountered this kind of JSON
{
"data": {
"Location": "[{\"id\":\"asdiqwe321\",\"name\":\"Manila\",\"lat\":25.42952,\"long\":-96.7960712,\"rating\":3,\"address\":\"Manila Ph\"},{\"place_id\":\"zzxdasdqwe1235as\",\"name\":\"Quezon City Ph.\",\"lat\":12.523562,\"long\":24.663415,\"rating\":1,\"address\":\"Quezon City Ph\"},{\"place_id\":\"rtiDFSDQ1321\",\"name\":\"Makati\",\"lat\":32.151236,\"long\":21.24124561,\"rating\":3.5,\"address\":\"Makati PH\"}]"
}
}
I have a model and want to cast it there:
export interface Location{
place_id: string,
name: string,
lat: number,
long: number,
rating: number,
address: number
}
This is my first time to encountered this type of JSON. I hope someone will help me and understand what is it.
That's not a JSON Object, that's a string. The \ character is used to escape the double quote ", otherwise JavaScript would interpret the double quote as the end of the string and would throw a parsing error.
If you want to access the string as an object, you need to parse it using JSON.parse:
const obj = {
"data": {
"Location": "[{\"id\":\"asdiqwe321\",\"name\":\"Manila\",\"lat\":25.42952,\"long\":-96.7960712,\"rating\":3,\"address\":\"Manila Ph\"},{\"place_id\":\"zzxdasdqwe1235as\",\"name\":\"Quezon City Ph.\",\"lat\":12.523562,\"long\":24.663415,\"rating\":1,\"address\":\"Quezon City Ph\"},{\"place_id\":\"rtiDFSDQ1321\",\"name\":\"Makati\",\"lat\":32.151236,\"long\":21.24124561,\"rating\":3.5,\"address\":\"Makati PH\"}]"
}
};
const locationString = obj.data.Location;
const locationObject = JSON.parse(locationString);
console.log(locationObject[0].id);
If you don't parse it, instead, obj.data.Location is just a string:
const obj = {
"data": {
"Location": "[{\"id\":\"asdiqwe321\",\"name\":\"Manila\",\"lat\":25.42952,\"long\":-96.7960712,\"rating\":3,\"address\":\"Manila Ph\"},{\"place_id\":\"zzxdasdqwe1235as\",\"name\":\"Quezon City Ph.\",\"lat\":12.523562,\"long\":24.663415,\"rating\":1,\"address\":\"Quezon City Ph\"},{\"place_id\":\"rtiDFSDQ1321\",\"name\":\"Makati\",\"lat\":32.151236,\"long\":21.24124561,\"rating\":3.5,\"address\":\"Makati PH\"}]"
}
};
console.log(typeof obj.data.Location);
You usually need to stringify or parse objects for serializations purpose.
It is Array of object of type Location in serialized form.
var response = {
"data": {
"Location": "[{\"id\":\"asdiqwe321\",\"name\":\"Manila\",\"lat\":25.42952,\"long\":-96.7960712,\"rating\":3,\"address\":\"Manila Ph\"},{\"place_id\":\"zzxdasdqwe1235as\",\"name\":\"Quezon City Ph.\",\"lat\":12.523562,\"long\":24.663415,\"rating\":1,\"address\":\"Quezon City Ph\"},{\"place_id\":\"rtiDFSDQ1321\",\"name\":\"Makati\",\"lat\":32.151236,\"long\":21.24124561,\"rating\":3.5,\"address\":\"Makati PH\"}]"
}
}
You have to parse it.
var locationData : Array<Location> = JSON.Parse(response.data.Location);
I'm using Groovy, i've tried to create a simple function which will construct a Json object from a provided Json string, then i'm trying to print this string but unfortunate it's adding Square brackets to the output.
Here's a snippet from my code:
def JsonBuilder ConstructJsonObject (jsonStr) {
def jsonToReturn = new JsonBuilder();
def root = jsonToReturn(jsonStr);
return jsonToReturn;
}
String jsonStr = "{id: '111'}";
println(jsonStr);
def jsonObject = ConstructJsonObject(jsonStr);
println(jsonObject.toPrettyString());
And here's the output:
{id: '111'}
[
"{id: '111'}"
]
It's returning an Array and not a pure Json.
If you change your input to be valid json (with double quotes round the keys and values), you can do:
import groovy.json.*
String jsonStr = '{"id": "111"}'
println new JsonBuilder(new JsonSlurper().parseText(jsonStr)).toPrettyString()
To print
{
"id": "111"
}
var jsonElements = List[String]()
val params = Map("host"->host)
for((key,value)<-mapSql){
val map = Map("metric"->key,"timestamp"->new Date().getTime,"value"->value,"tags"->params)
jsonElements=JsonUtility.toJSONString(map) :: jsonElements
}
val entity = new StringEntity(JsonUtility.toJSONString(jsonElements))
println("json elements final list is "+jsonElements)
println("json elements final JSON Obj is "+JsonUtility.toJSONString(jsonElements))
entity.setContentType(new BasicHeader("Content-Type", "application/json"))
val postRequest: HttpPost = new HttpPost(putURL)
postRequest.setEntity(entity)
val postResponse: CloseableHttpResponse = httpclient.execute(postRequest)
I basically need to add values to a list and then send them together in a JSON Array.
However this is introducing unnecessary escape characters "/" in the output which is rendering the post request useless and I am getting an error to the API hit. the following is the response :
json elements final list is List({"metric":"replicationLag","timestamp":1410179907871,"value":0.0,"tags":{"host":"tg-em-db01.nm.xxxx.com"}}, {"metric":"status","timestamp":1410179907824,"value":1,"tags":{"host":"tg-em-db01.nm.xxxxx.com"}})
json elements final JSON Obj is ["{\"metric\":\"replicationLag\",\"timestamp\":1410179907871,\"value\":0.0,\"tags\":{\"host\":\"tg-em-db01.nm.xxxx.com\"}}","{\"metric\":\"status\",\"timestamp\":1410179907824,\"value\":1,\"tags\":{\"host\":\"tg-em-db01.nm.xxxxx.com\"}}"]
I can replace and remove all the escape characters by the replaceAll function but I do not want to do that. is there a better way to append objects to an already existing JSON object and then change it to an array ( which i can easily do by new JsonArray(List(JsonObj)) ) so that i dont get any escape characters anywhere.
Something like this :
val params = Map("host"->host)
var map = Map[String,Any]()
for((key,value)<-mapSql){
map ++= Map("metric"->key,"timestamp"->new Date().getTime,"value"->value,"tags"->params)
}
val entity = new StringEntity(JsonUtility.toJSONString(List(map)))
println("json elements final list is "+map)
println("json elements final JSON Obj is "+JsonUtility.toJSONString(List(map)))
is giving me this as an ouput :
json elements final list is Map(metric -> replicationLag, timestamp -> 1410180939983, value -> 0.0, tags -> Map(host -> tg-em-db01.nm.xxxx.com))
json elements final JSON Obj is [{"metric":"replicationLag","timestamp":1410180939983,"value":0.0,"tags":{"host":"tg-em-db01.nm.xxxxx.com"}}]
But I need something like this :
[ {"metric":blah blah} , {"metric":blah blah} ]
Is there a way to append to maps such that the same key values are not clubbed ?
Thanks in advancE!
var jsonElements = List[Map[String, Any]]()
val params = Map("host" -> host)
for ((key, value) <- mapSql) {
val map = Map("metric" -> key, "timestamp" -> new Date().getTime, "value" -> value, "tags" -> params)
jsonElements = map :: jsonElements
}
val entity = new StringEntity(JsonUtility.toJSONString(jsonElements))
entity.setContentType(new BasicHeader("Content-Type", "application/json"))
val postRequest: HttpPost = new HttpPost(putURL)
postRequest.setEntity(entity)
val postResponse: CloseableHttpResponse = httpclient.execute(postRequest)
object JsonUtility {
def toJSONString(obj:Any):String = {
compact(JsonAST.render(decompose(obj)))
}
}
I am trying to parse some JSON objects which is made just of (string,string) pairs. The file I am parsing contains this. I want ot emulate resjson behaviour.
{
"first_key": "first_value",
"unicode": "\u0040 #"
}
What I do is
string path = #"d:\resjson\example.resjson";
string jsonText = File.ReadAllText(path);
IDictionary<string, string> dict;
try
{
dict = JsonConvert.DeserializeObject<IDictionary<string, string>>(jsonText);
}
catch(Exception ex)
{
// log or something
}
When I obtain the dict object, the
foreach (var pair in _dict)
{
string key = pair.Key;
string value = pair.Value;
Console.WriteLine("Key = '{0}', Value = '{1}'", key, value);
}
This inputs for me :
"Key = 'first_key', Value = 'first_value'"
"Key = 'unicode', Value = '# #'"
Once Newtonsoft.Json deserializes the object, I lose the "\u0040" string and I have no way of knowing how the original file looked like. Is there a way to preserve character escaping ?
Well, one simple idea would be to escape all the backslashes in the original text before passing it to the parser:
dict = JsonConvert.DeserializeObject<IDictionary<string, string>>(
jsonText.Replace(#"\", #"\\"));
Using Newtonsoft.Json, you can actually do:
var settings = new JsonSerializerSettings()
{
StringEscapeHandling = StringEscapeHandling.EscapeNonAscii
};
var json = JsonConvert.SerializeObject([obj or JToken], settings);
It won't show the original string as that is lost when deserializing, but it will encode all non ascii characters as \uXXXX