Validate and parsing JSON with Argonaut - json

I need parse some JSON into Scala instance or into error JSON.
For example if I have next JSON:
{"user":"us","password":"pass"}
and I have constraint that "user" shouldn't be less than 3. And then I should get next error JSON:
{"user":"us", "_error_user":"Username length shouldn't be less than 3","password":"pass"}
But for JSON {"user":"user","password":"pass"} I should get some Scala instanse like User("user","pass").
Is it possible using Argonaut?

Related

Retaining unknown keys during JSON (de)serialization with Codable

I'm using Codable to convert between JSON and a Swift object. The process goes like this:
JSON -> Swift object
Modify the Swift object
Swift object -> JSON
The issue is that if the original JSON contains unknown keys, they are removed during the conversion to the Swift object and then back to JSON. Is there a way to retain these unknown keys during the conversion process?

Attaining the data in json format from the payload which is available as "org.mule.munit.common.util.ReusableByteArrayInputStream#53534c15" in mule 3

I need the real payload json data to be able to assert it against another hardcoded json file in munit (mule 3.9 and dataweave 1). The issue is the payload show as "org.mule.munit.common.util.ReusableByteArrayInputStream#53534c15" under payload. When I convert it to java I can see the data, but not in json format. How can I extract the json in this byte array stream to be able to assert it against a json hardcoded file.
I resolved it by using the "Byte to String" block
Then, I have added the "Assert Equals" block, but made sure to format both values likes this.
#[payload.replaceAll("\\s+","")]
#[getResource('sample.json').asString().replaceAll("\\s+","")]
This did exactly what I needed.

Is it possible to deserialize data that looks like JSON (but isn't) using serde_json?

I have a hard time deserializing (using Rust's serde and serde_json v1.0) the following JSON I receive:
{
["string content"]
}
The object's array is not identified by a key, so the following doesn't work:
#[derive(Deserialize)]
struct Data {
key: Vec<String>
}
I've also tried using #[serde(flatten)] on the key field but I get an error:
can only flatten structs and maps (got a sequence)
The data I receive doesn't look like valid JSON. Is it still possible using serde_json?
The input you show is not valid JSON. You will not be able to use serde_json to deserialize that input because serde_json only accepts JSON.
If you find out what format the data is intended to be in, consider using (or writing) a Rust library dedicated to that specific format.

Parsing Complex JSON in Scala

I have the below JSON field in my data.
val myjsonString = """[{"A":[{"myName":"Sheldon""age":30"Qualification":"Btech"}]"B":"UnitedStates"},{"A":[{"myName":"Raj""age":35"Qualification":"BSC"}]"B":"UnitedKIngDom"},{"A":[{"myName":"Howard""age":40"Qualification":"MTECH"}]"B":"Australia"}] """
The parse method gives the following structure:
scala > val json = parse(myjsonString)
json: org.json4s.JValue = JArray(List(JObject(List((A,JArray(List(JObject(List((myName,JString(Sheldon)), (age,JInt(30)), (Qualification,JString(Btech))))))), (B,JString(UnitedStates)))), JObject(List((A,JArray(List(JObject(List((myName,JString(Raj)), (age,JInt(35)), (Qualification,JString(BSC))))))), (B,JString(UnitedKIngDom)))), JObject(List((A,JArray(List(JObject(List((myName,JString(Howard)), (age,JInt(40)), (Qualification,JString(MTECH))))))), (B,JString(Australia))))))
I am trying to parse it using Scala json4s. Visited almost all previously asked questions related to this, however, could not get proper solution to this. The O/P should be something like this:-
UnitedStates 30
UnitedKIngDom 35
Australia 40
or only the age in 30#35#45 format.
The JSON you posted is invalid, there are missing commas between your object fields.
In order to get the output that you want you will need to extract the data from the parsed AST that Json4s will create upon successful parsing of the data. Json4s provides a number of ways with which you can manipulate and extract data from a parsed AST.
You could map over the list of objects inside the JArray and extract the country and age from each object. I don't wish to provide code to do this, as you haven't provided an example of what you have tried to do other than simply parsing the JSON string.

Jackson filter its response based on JSON field

I parse about ~300kb (it might grow) json file, so instead of using default Android's JSON parser I've used Jackson. However, I have to filter it's response based on value of field (from JSON's object). Example:
Json File:
{"foobars":[{"foo":1,"bar":1},{"foo":2,"bar":2}]}
and the response will be an ArrayList (in my case) with these two objcets. However I'd like to get the objects only if "foo" < 2. How to do that?