Jolt: Json string literal to json object - jolt

I am looking for the way to transform JSON string to Json object.
I have a Json
input:
{
"item":"{\"currency\":\"USD\",\"content\":\"12\" TV\"}"
}
Is there a way to transform the string to json object using Jolt?
expected output:
{
"item":{"currency":"USD","content":"12\" TV"}
}
Thanks
David

Related

How to convert mongodb document to JSON string?

I am using mongodb rust driver, But I don't know how to convert mongodb::bson::Document to JSON format.
let document: mongodb::bson::Document = client
.database("demo")
.collection("folder")
.find_one(doc!{})
.await?
.unwrap();
println("{:?}", document); // How to convert `document` to json string?
I want to send this JSON string from server as response.
mongodb::bson::Document implements Serialize from serde, so you can use serde_json::to_string, or other functions from serde_json to serialize the data to JSON.

Convert date format from json data in the model and then pass to controller

Here in my model I have a few struct to handle the data from server
struct Articles: Decodable {
var content : [ArticleData]
}
struct ArticleData: Decodable {
var title: String
var date_time: Date
var image: String
}
From the jason, I get a date, it's like that:
"date_time": 1524842056.035,
First question:
I set the type of this type in model Date as you see in ArticleData, it that correct?
Then I read this json in the model something like that:
guard let json = try? JSONDecoder().decode(Articles.self, from: data!) else {
completion(.failure(loginError.networkError))
return
}
completion(.success(json.content))
As you can see, I get an array from server json.content that contain title, date_time and image. I want to convert it here in the model and pass everything in completion to the controller.
could you help me on that?
Thanks :)
you can use type 'String' for your date and then convert it using dateFormatter or, use 'dateDecodingStrategy' function in your decoder.
for passing the data you are already doing the correct thing. where ever you call the method you can get the model.

load from json array into object array swift

I have array of user
var User:[User] = []
I want by alamofire and swiftyjson get info from http://jsonplaceholder.typicode.com/users
I know how to request and then i have array of json
how i can make loop through json array and create object of user then append in array User[]
I think my problem with loop , this is from swifty json page
for (index,subJson):(String, JSON) in json {
//Do something you want
}
how i can use it in my App?
You can use similar following code
for (_, subDataJSON): (String, JSON) in dataJSON {
let u = User()
u.name = subDataJSON["name"].stringValue
u.id = subDataJSON["id"].intValue
u.active = subDataJSON["active"].boolValue
self.User.append(u)
}
Parse your data json in loop then at the end of loop append single user object to global class User array like that.
Then use your class scoped User array like as you want.

Cannot Subscript Value of Type JSON to an Index of Type JSON Error in Swift

I am using Wunderground to gather history data, using SwiftyJSON. However, in the API, a section called "dailysummary" is curtained off in an array. Abbreviated, it looks something like this:
"history": {
"dailysummary": [{
"stuff": "here"
]}
I had tried to isolate the content inside the array with this:
var jsonData = json["history"]["dailysummary"]
var arrayData = json["history"]["dailysummary"][0]
jsonData = json["history"]["dailysummary"][arrayData]
This code returns an error saying it cannot subscript a value of type 'JSON' with an index of type 'JSON'. Is there any way to make this work, or a way to get the data from to array in this format?
You want something like:
var arrayData = json["history"]["dailysummary"][0]["stuff"].
arrayData is a dictionary.

Retrieve Parse Array of Objects in Swift

I'm storying an array of JavaScript key/value objects in a column of type Array on Parse like this:
[{"1432747073241":1.1},{"1432142558000":3.7}]
When I retrieve that column in Swift, I can see the data, but I'm unsure what data type to cast it as:
if let data = dashboardObject[graphColumn] as? [AnyObject]{
for pair in data{
println(pair)
}
}
That print yields this in the console (for the first pair):
{
1432747073241 = "1.1";
}
I can't seem to cast its contents as a Dictionary [Int:Double] and I'm guessing that means this is a string.
How do I parse this data in Swift? Thanks.
The Dictionary you should parse it to is [String: AnyObject]. It seems as if the keys of this dictionary are timestamps which you probably don't know. You could iterate through the dictionary like this:
for (key, value) in pair {
// do what you want in here with the value and/or the key
}