How to save SwiftyJson string array into string array - json

How can i save the SwiftyJSON String array into String of Array in swift
let jsonStr: JSON = JSON(socketDictionary.myStructValue)
print(jsonStr)
The output is:
["1CZ3isxgnmrY8w1eUJmCjNSm2fvhfXYkNQ","12ZKF9uZnaz5Wk6wVvmphE42LjM5M51upV","1LzPBqfrsdxDeFkeZqSGuaS3zxw8wTRQXx","1Z5qe2NSZyzvoNbmU7tuB7AaH7i4JQJXJ","15BKJq6Q2QHdw8v93JwBqy5GKrbLDWbbUR","1DCVJrzfZrFxqBGRF3H744SB1dC8aw5Rgn","1QGsCPM2HLfAMyFdfWddzijbzXF6cwm7RA","1QGW6T2KLTuWuu5qkfmZ8E63amtZmasRSu","1CLSGF92T8936tfm9qKG4N6cQir7AKSM7Q","1C7wnv7TBaX3H3xeHycr95h9mzzvVUq9Wz","1vBegRfLZbiwyEZ9LXo4d1KbLnUEyZLpF","1Nh9mxHuXF2T6V8qwHf95T6G24EFaQRG2M","16S2TgTX4ArtNW2htUCdCk6KvAZZFfw8Xi","1J3f1UoAGwmbK1aN9vTYex5rjpDnDewtTH","1BsawvNcAs289FZv3LbZY6STweTVJk6DeW","1LSQBT7QuBBSkwyFZpsxoEuw3oBDe57DT1","1LUSG4Pm7FiiCBUcEjWduLsCjswPheHZyF","1KPD18LDsmemHUR8TyLc8C3LJRKTgxVFe5","1FASJSpNjLVTEqmM72KjrZ85H7GudbQxZn","1MZxxP5YjLh29W7tgcrtYc2ywuAjdB2FC4","1JakuQCqpcmz1husavN8dHFGQyy3yDRCrD","177D2KQ4gd6kW1b4jTs9TLjUAV927F6Wc4","1PgKwdsKBqFNsZz4JcZ82kqBXRpCbWjPae","1P2iYhmVxhg3W8tEeQQK23NY5mKxHPbNnQ","14AgWBp6XYw12cBdAvdwz3sh9issWX7UnW","1DMPZjYf9LvHe3p7ZvZTVYsasGJps5QWWu","1MncjTfvdkNp1P1z22LAgbXU8mmTi59sLw","1PLLzt7Jiww6KqfsRwM6aW4z6pHoJNiDLR","1JvGhkZ26hy1Tjmxyx8C9amzo3pt6Ecxfh","1D3P1oYE9PBJhz9HYXeVJwuM5Qgg6RdYRu","1DGaE7K1nPjoVuL7BD4y3NWZvQ8gGNzenj","1h81XFB5nLsRJu2UtPinJx3niv1LMWSj5","18tAQ6awwcecSbxT63kQBmyJm7hWQLDppe","17p9vLfD1Q3Hdxp5zCNuX4cSe7TDjvzDXc","1GAsmp3j26Y6AkMG1jtVoZFBxZnJMSWQLF","1MJa3EFHFJwY2myibcVWiB6zuofFFaBWAR","19FwYXp1vfgU7bNojsggkuVZsiiAKRxbF7","18tG7Czk2akY2qbcn651CwCWzv9QA3DKUn","1AzacPufLFp7tL593Yimg7cnu4SymganbL","1EifaT43avdWcsXMhyVHvPmtNz7YyfDEnZ","17Xax4sY5Z694QQEKKz1ioaSrxXujnoa1y","18G4HWStJBVE4k4uLnif5cKAsyXpKx3VqY","1FUKc1zh8ia2h1j8Z1DqTTVQ2cJSyx7AnW","1HQq9mQxePKZg8eDncggx9LZ3ufc6sYSPp","14mKUH5jFmEW6DiKAiGJmeztqZ46XQkxch","1JWJwbiPzehXz8D5s7jTddEDxtMLBaFfkV","1GZbEdUjBhqZfp1JAyaoCZRTCiXSa4gbty","1D69wSHc1wK778gYWogzrm2hnerCBs1ZC6","12aa3sB1DTQDGT5UQHWnU71ziLLvQHKLTr","1AF2rxRKJbtUTNH4iyX5csrrG66Y723YwP","1AR77rXiZga2XUd2W7Wk1jZQfGjtK3gDBs","1PVJqkWLEed11uHHBeH3x4ya6zSjS7sv2e","168VzjYSz3tYaYLLoF28WKoc2NzBTAM866","13SnzBJL7jrHfPCWSAS8GD4ctmKAfgvTaa","16KBupqzu8hUCzqjdgr6ZFfM9TFnRkz82u","1Pd7NY5onBAQtwjf9aKGaun8QDhuoFf7jr","1LM65ijpT6SnTwQHd6bz44RZAaaVXEmT8x","1Mzg7bLFoA5wrDT2gUk7M9JPA7yxGYGN7c","1N7Tw2KDaUWRexGCoNSa5ecZkDHdvNRw1Z","1GPRVhFa1X99VqYJuRR6wBP1vz1txWiNYQ","1JaaeGaNeytwTPh9Dwv9XdcUyfzmhDVi6a"]
i want to save this json output array to simple array and then iterate those array

Just use dicionaryObject from SwiftyJSON. This will convert JSON value into [String: Any].
Ex.: jsonStr.dicionaryObject

Related

how to parse below string 2 arrays

I need to parse the below json string please help how to parse
I have 2 array.
[{"BuyerHeatMap":{"BuyerId":"1022","Region":"Asia","SupplierCount":9,"Color":"#fff302"}},{"BuyerHeatMap":{"BuyerId":"1022","Region":"Europe","SupplierCount":100,"Color":"#de930a"}}]

Printing out MongoDB Documents in JSON Format

I am retrieving documents from my mongoDB database, but I need to print it out in JSON format. I store all of the relevant information in an array as the MongoKitten documentation suggests. I want to print out the entire contents of each element in the array, which is remember a document from MongoDB. My code is as follows:
import Foundation
import MongoKitten
let myDatabase = try MongoKitten.Database("mongodb://taylor:starwars1#ds129374.mlab.com:29374/taylorswiftengine")
let myCollection = myDatabase["my_collection"]
Request.addHandler(forMethod: "GET", withRoute: "/:resource/:id1")
{
(routeParams:RouteParams) in
let myTopics = try! myCollection.find("topic" == "\(routeParams["id1"]!)")
let allTopics = Array(myTopics)
}
MongoKitten comes with an Extended JSON module, which you can import:
import ExtendedJSON
You can convert an array of documents ([Document]) to Extended JSON using makeExtendedJSON():
myArrayOfDocuments.makeExtendedJSON()
This returns a Cheetah.Value. Cheetah is OpenKittens JSON library. To get a JSON String, you can use the serializedString() method on Cheetah.Value.
To sum things up, use this to convert your array of BSON Documents to a JSON String:
myArrayOfDocuments.makeExtendedJSON().serializedString()
If your myTopics object a dictionary you could convert it to json like this:
let dict = ["key1": "B", "key2": "A", "key3": "C"]
let jsonData = try JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
Otherwise you could manually convert the myTopics object to a [String:String] dictionary and than convert it to json like above but I'm not sure if thats the way to handle with mongodb objects.

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

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