Convert String to JSON String in Swift - json

How can I convert string with format like:
{Param1: "Value", Param2: "value2", Param3: "value3"}
to JSON string for my Post query?
Thanks in advance.

This is already a JSON string just send it appended with the headers.
If you need to access these values, then you need to use something like swiftyJSON or JSONswift.

What you have posted appears to be a JSON string containing a dictionary.

Related

Kotlin: JSON string with linebreaker and variable

I would like to add linebreakers to a JSON string so it will be more readable.
val myString = "some string"
val myObjectJson = `
{
"example1": "value",
"example2": $myString
}`
So later I can use ObjectMapper to create an object. objectMapper.readValue(myObjectJson, MyClass::class.Java)
What I'm trying to figure out:
How to add lineBreaker in Json string?
Tried template literals:"`", doesn't seem to work in Kotline. gives Expecting an expression error.
How to use variable in Json string?
This might go away after I figure out how to add linebreakers.
(Edited because I think I have misunderstood your question).
Are you asking how to lay out a JSON string in the source code? If so then what you are looking for is the Raw String feature implemented with 3 double-quotes like this:
val myObjectJson = """
{
"example1": "value",
"example2": $myString
}
"""
But in case you asking
How to add lineBreaker in Json string?
(Assuming you are using Jackson's ObjectMapper)
Simply use writerWithDefaultPrettyPrinter() like this: MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(value)
If that is not good enough for you I suppose you could implement a custom Pretty Printer: https://www.javadoc.io/static/com.fasterxml.jackson.core/jackson-core/2.14.2/com/fasterxml/jackson/core/PrettyPrinter.html

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

Flutter: Convert string to Map

I am using SQFlite to store data locally, I have a table in which I have a field called 'json', this field is of type TEXT and stores a json converted to String, such as: '{name: Eduardo, Age: 23, Sex: male}'.
Up to this point, everything works fine.
But then I need to consult this information from the database and how it is stored in a text type format flutter recognizes it as a string. I don't know how to convert it back to an object.
I know that I can build a function to solve this, in the case that the information stored in the json always complies with the same structure. But in my case, the information contained by the json will be variable.
Is there a way to solve this problem?
you can Simply use json.decode function from the dart:convert package.
example:
import 'dart:convert';
main() {
final jsonString = '{"key1": 1, "key2": "hello"}';
final decodedMap = json.decode(jsonString);
// we can now use the decodedMap as a normal map
print(decodedMap['key1']);
}
check those links for more details
https://api.dart.dev/stable/2.10.3/dart-convert/json-constant.html
https://api.dart.dev/stable/2.4.0/dart-convert/dart-convert-library.html
if you have issue that your json keys doesn't have quotes then try this code it transform the unquoted String to a quoted String and then decode it , it's 100% working
final string1 = '{name : "Eduardo", numbers : [12, 23], country: us }';
// remove all quotes from the string values
final string2=string1.replaceAll("\"", "");
// now we add quotes to both keys and Strings values
final quotedString = string2.replaceAllMapped(RegExp(r'\b\w+\b'), (match) {
return '"${match.group(0)}"';
});
// decoding it as a normal json
final decoded = json.decode(quotedString);
print(decoded);
#EduardoColon If the keys are not quoted, then it's not JSON, it's your own custom key-value format. JSON keys have to be quoted. See https://json.org for the "official" JSON syntax.

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.

KRL: Parsing string as JSON

After using http:get(), I receive back a string from picking the "content" from the hash:
response = http:get(webservice_url, {"key1": value1, "key2": value2});
json_resp = response.pick("$..content");
However, since json_resp is a string and not an actual JSON object, I can't run a command like this:
value = json_resp.pick("$..string");
Is there a way to tell KRL that I want to parse json_resp as JSON? An eval() or something, perhaps?
The decode() operator does just what you want. It operates on a JSON string, attempting to convert it to a native KRL object. Note that KRL also has encode() which operates on a native KRL object and returns a JSON string representation of that object.
response = http:get(webservice_url, {"key1": value1, "key2": value2});
json_resp = response.pick("$..content").decode();
value = json_resp.pick("$..string");
// will work since json_resp is now a native KRL object