I have the following JSON scalar:
"""
The `JSON` scalar type represents JSON values as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).
"""
scalar JSON
which I am trying to convert since my query is accepting input: JSON. When testing using graphql playground, query is JSON object thus the following works:
query {
carts(where: {
owner:{id: "xxx"}
store:{name: "yyy"}
}) {
id
}
}
# query is the starting from the where: {...}
However when using ferry, I have trouble converting the scalar JSON, I've tried to convert it to String and/or Map<String, dynamic> but both are throwing errors. I am trying to write my custom serializer following this link:
https://ferrygraphql.com/docs/custom-scalars/
however it is throwing the same error like when I'm trying converting it to Map<String, dynamic>, I have no idea what to convert to as dart do not have any type of JsonObject and usually Map is suffice for json representation. Any idea guys? I'd appreciate any clues as I've exhausted all of my options and am currently bruteforce trying
Related
overall aim
I have data landing into blob storage from an azure service in form of json files where each line in a file is a nested json object. I want to process this with spark and finally store as a delta table with nested struct/map type columns which can later be queried downstream using the dot notation columnName.key
data nesting visualized
{
key1: value1
nestedType1: {
key1: value1
keyN: valueN
}
nestedType2: {
key1: value1
nestedKey: {
key1: value1
keyN: valueN
}
}
keyN: valueN
}
current approach and problem
I am not using the default spark json reader as it is resulting in some incorrect parsing of the files instead I am loading the files as text files and then parsing using udfs by using python's json module ( eg below ) post which I use explode and pivot to get the first level of keys into columns
#udf('MAP<STRING,STRING>' )
def get_key_val(x):
try:
return json.loads(x)
except:
return None
Post this initial transformation I now need to convert the nestedType columns to valid map types as well. Now since the initial function is returning map<string,string> the values in nestedType columns are not valid jsons so I cannot use json.loads, instead I have regex based string operations
#udf('MAP<STRING,STRING>' )
def convert_map(string):
try:
regex = re.compile(r"""\w+=.*?(?:(?=,(?!"))|(?=}))""")
obj = dict([(a.split('=')[0].strip(),(a.split('=')[1])) for a in regex.findall(s)])
return obj
except Exception as e:
return e
this is fine for second level of nesting but if I want to go further that would require another udf and subsequent complications.
question
How can I use a spark udf or native spark functions to parse the nested json data such that it is queryable in columnName.key format.
also there is no restriction of spark version, hopefully I was able to explain this properly. do let me know if you want me to put some sample data and the code for ease. Any help is appreciated.
I am using Kotlin and I have a service that is getting an object of type Any (this cannot be changed)
The problem with Any is that is an object of 20+ fields and I just need one of them to use it as a filter... therefore I cannot do a simple cast.
So, my object is like: (when I print it)
{messageId=123, userId=32323, address=Some city, phone=111605,type=TYPE1.....
I want to convert it using Kotlinx or Jackson but I cannot convert it first to the expected String format, doing a parseFromString(myObject) will result in an exception as well of a wrong Json format.
I want to convert it to a class like this
#Serializable
private data class UserType(val type: String)
type is the only field I care about.
My convertion is via kotlinx
val format = Json { ignoreUnknownKeys = true }
format.decodeFromString<UserType>(myObject)
I even tried this to see if I can make it in the proper Json format
format.encodeToString(original)
Any idea what I could do here that would be a lightweight solution?
This is my Any type https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/
I am told I need to communicate in flutter over the web using JSON. I know how to bring in JSON data and convert it into a Dart object. How am I expected to put out the JSON? Should I output the JSON in the form of a Dart object? How does that work? I've tried to do research but can't seem to find the answer.
You can add a method to your class that serialized it into a dictionary like this:
class Car {
final int nWheels;
final String color;
Car(this.nWheels, this.color);
Map<String, dynamic> toMap() => {
"nWheels": this.nWheels,
"color": this.color,
}
}
The resulting map can then be converted to a JSON string by using the flutter json library. That would look like this:
Car car = Car(4, "blue-ish");
String json = jsonEncode(car.toMap());
json is now a JSON-encoded String that can be transmitted to the server.
I am using .NET 4.0, MVC 4, Web API. I have following data structure:
Dictionary<Actor, Int32> TopActorToMovieCount = new Dictionary<Actor, Int32>(10);
And following entry in WebApiConfig:
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
In my Controller, I am returning TopActorToMovieCount this way:
[HttpGet]
public HttpResponseMessage HighestMovies()
{
return Request.CreateResponse(HttpStatusCode.OK, MvcApplication.TopActorToMovieCount);
}
But the JSON output it is giving is:
{"api.Models.Actor":137,"api.Models.Actor":125,"api.Models.Actor":99,"api.Models.Actor":96,"api.Models.Actor":83,"api.Models.Actor":82,"api.Models.Actor":81,"api.Models.Actor":79,"....
Why it is not giving JSON structure for object of Actor?
I am sure that I am missing something, bout couldn't figure out. I tried adding following, but it didn't work:
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
PS: When I switch to XML output, it works fine.
See similar question here: Not ableTo Serialize Dictionary with Complex key using Json.net
In this case, you are using "Actor" as the Key of your dictionary. Dictionary stores key/value pairs. So when creating the JSON response, it interprets the "Actor" as a key which is converted to a string, and the "Int32" as the value thus giving you
{"api.Models.Actor":137} or {key:value}
because
Actor.ToString() would result in "api.Models.Actor"
Here's a link to the definition of Dictionary: https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx
I've 2 json feeds in payload (using Gather), i planned on using a groovy script to make it into a single json (I expected something like:
{key:value}{key:value})
<scripting:transformer doc:name="Groovy">
<scripting:script engine="Groovy"><![CDATA[return '{"data":['+payload.toString().replace("}{","},{"+']}']]></scripting:script>
</scripting:transformer>
(expected output: {"data":[{key:value},{key:value}]}
But i get:
{"data":[[org.glassfish.grizzly.utils.BufferInputStream#102e37e, org.glassfish.grizzly.utils.BufferInputStream#a569d1]]}
W/O groovy script:
[org.glassfish.grizzly.utils.BufferInputStream#102e37e, org.glassfish.grizzly.utils.BufferInputStream#a569d1]
an array of inputstream
I tried using byte array to string, and object to string but it doesnt work, I dont figure out how can i solve this
Replace:
payload.toString().replace("}{","},{")
with:
payload.collect { it.text }.join(',')
Explanation: .text deserializes an input stream to a string, so payload.collect { it.text } will yield a collection of strings. Then join(',') takes care of concatenating these strings, separating them with ,