how to parse below string 2 arrays - json

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"}}]

Related

JSONPath: Get field starting with # in escaped json string

I want to get a nested field in a json string using JSONPath.
Take for example the following json:
{
"ID": "2ac464eb-352f-4e36-8b9f-950a24bb9586",
"PAYLOAD": "{\"#type\":\"Event\",\"id\":\"baf223c4-4264-415a-8de5-61c9c709c0d2\"}"
}
If I want to extract the #type field, I expect to do it like this
$.PAYLOAD.#type
But that doesn't seem to work..
Also tried this:
$.PAYLOAD['#type']
Do I need to use escape chars or something?
Posting my comment as an answer
"{\"#type\":\"Event\",\"id\":\"baf223c4-4264-415a-8de5-61c9c709c0d2\"}"
Isn't JSON, it's a string containing encoded JSON.
Since JsonPath can't decode such string, you'll have to use a language of your desire to decode the string.
Eg: Decoding JSON String in Java

How to save SwiftyJson string array into string array

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

GSON : Parsing JSON when keys are a part of response

Here's the response I am trying to parse.
{"table":"PypersonResponsibleCrewMembersWklyView","numberOfRemainingRecords":0,"rowDefinition":{"attrNames":["EmpOraseq","{PEmpNo","PEmpName","Code","Name","EmpNo","EmpTrdCode","EmpTrdName","EmpFirstName","EmpLastName","NhHrsTot","OtHrsTot","DotHrsTot","NhHrs1","OtHrs1","DotHrs1","NhHrs2","OtHrs2","DotHrs2","NhHrs3","OtHrs3","DotHrs3","NhHrs4","OtHrs4","DotHrs4","NhHrs5","OtHrs5","DotHrs5","NhHrs6","OtHrs6","DotHrs6","NhHrs7","OtHrs7","DotHrs7"]},"rows":[{"attrValues":[211448,"1016","Mr. Demo","REL","aman","1004","PRES","President","Gord","Rawlins",0.5,0.5,0,0.5,0.5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},{"attrValues":[211448,"1016","Mr. Demo","REL","aman","ESS001","PROE","Project Engineer","Nadal","Rafael",0.5,0.5,0.5,0.5,0.5,0.5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},{"attrValues":[211448,"1016","Mr. Demo","REL","aman","CL1001","SS01","System Specialist","Ronald","Bromell",0.5,0.5,0.5,0.5,0.5,0.5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}]}
In this response 'attrNames' contains the keys and 'attrValues' contains the values. Is it possible to parse this using GSON?

How to convert a String array to Swiftyjson JSON type

I need to pass an array of string/Int (doesnt matter) as JSON parameter in a HTTP body for a POST request.
{
"par1" : value,
"par2" : "value2",
"par3" : ['123:456', '123:234' ...]*
}
*My problem is for filling my JSON object with param3 values. In the swift code I have them as an array of Strings.
There are many example of converting a JSON object back to an array of strings/Int, but I can't find it the other way around.
As mentioned in the comments, the way to go is to use NSJSONSerialization to generate object encoded in JSON data.

Jettison can not convert json string to json object when there is an element has empty string value

My application is using camel rest (2.15.2) to catch a POST json String and then uses jettison to convert to a JSON Object. It is working fine with normal request.
POST request: {"request"={"lname"="aaa", "fname"="bb"}}
1. String body = exchange.getIn().getBody(String.class);
2. JSONObject obj = new JSONObject(body);
When i debug, variable body = {request={lname=aaa, fname=bb}}.
And line 2 returns a JSONObject. so far so good
if we try to another the request:
{"request"={"lname"=" ", "fname"="aa"}}
then body = {request={lname= , fname=aa}}
line2 returns Exception.
Could you please help me to fix this issue: convert json string which contains element has empty value string to json object.
The above request is acceptable in my scenarios.
Error:
org.codehaus.jettison.json.JSONException: Missing value. at character
15 of {request={lname= , fname=aa}} at
org.codehaus.jettison.json.JSONTokener.syntaxError(JSONTokener.java:463)
at
org.codehaus.jettison.json.JSONTokener.nextValue(JSONTokener.java:356)
at org.codehaus.jettison.json.JSONObject.(JSONObject.java:230)
at
org.codehaus.jettison.json.JSONTokener.newJSONObject(JSONTokener.java:412)
at
org.codehaus.jettison.json.JSONTokener.nextValue(JSONTokener.java:327)
at org.codehaus.jettison.json.JSONObject.(JSONObject.java:230)
at org.codehaus.jettison.json.JSONObject.(JSONObject.java:311)