How to extract json string value in scala - scala

So i have a json file in which i have schema like this:
{
"Field": "{\"key1\":\"Value1\",\"key2\":\"Value2\"}
}
I want to extract value of key1 & key2 which is Value1 & Value2

Related

How to specify unique together index in gorm?

type ABC struct {
ID uint
Abc int `gorm:"unidqueIndex;
Bcd string
}
I want Field Abc and Bcd to be unique together
{Abc: 1, Bcd: "hello"} {Abc: 1, Bcd: "hi"} should be valid but
{Abc: 1, Bcd: "hello"} {Abc: 1, Bcd: "hello"} should be invalid
Give both fields the same index name.
type ABC struct {
ID uint
Abc int `gorm:"uniqueIndex:abc_bcd_uniq"`
Bcd string `gorm:"uniqueIndex:abc_bcd_uniq"`
}
See composite indexes in the GORM docs.

JSON array to Swift Array

I'm fairly new to Swift and trying to get a JSON array that comes back from Alamofire into a normal Swift array.
var dpoParamArr = JSON([])
//Alamofire code here and I get the value back below which is Parameters
self.dpoParamArr = JSON(dictMain)["Parameters"]
print(self.dpoParamArr.arrayValue.map({($0.dictionaryValue["name"]?.stringValue)!}))
print(self.dpoParamArr.arrayValue.map({($0.dictionaryValue["value"]?.stringValue)!}))
This is the print result:
["PAY_REQUEST_ID", "CHECKSUM"]
["5FB1DDBB-D637-3DC3-C0AF-288EFF98012C", "4FD856821E0F6D238F8346175227FF04"]
How do I get my array to have a key and value
"Parameters": [
{
"name": "PAY_REQUEST_ID",
"value": "94DEE72B-F75C-453F-1280-F1B26BBFD98E"
},
{
"name": "CHECKSUM",
"value": "9171405E05C9C9B7D4B6FF497FC4AE50"
}
]
My end goal is to get the name and value variables for both JSON nodes.
It's look like you are using Alamofire with SwiftyJSON. Below is some simple way to convert json to swift array.
1.Convert Json array to swift array.
if let swiftArray = self.dpoParamArr.arrayObject as? [[String:Any]] {
// swiftArray is array of dictionary
for item in swiftArray {
// item is a dictionary and you will get its value by key
// here how to get value by key
let name = item["name"] as? String
let value = item["value"] as? String
//printing the values
print("Name is: ", name)
print("Value is: ", value)
print("=========================")
}
}
You can also use like below
for item in self.dpoParamArr.arrayValue {
//here how to get value by key
let name = item["name"].stringValue
let value = item["value"].stringValue
//printing the values
print("Name is: ", name)
print("Value is: ", value)
print("=========================")
}
You can use SwiftyJSON for parsing. SwiftyJSON is a simplified JSON parsing library that gives you clearer syntax than the built-in iOS libraries. To detail information please read its document

Dictionary init(grouping:by:), 2 levels - simplifying code

Dictionary(init(grouping:by:)) works fine when I need to group array elements by some property.
What I get is:
{
key1: [
value1,
value2,
value3
],
key2: [
value4,
value5,
value6
]
}
However, I need to transform the array further, grouping every partition into smaller groups so, that the resulting data structure is having two layers:
{
key1: {
anotherKey1: [
value1,
],
anotherKey2: [
value2,
value3]
},
key1: {
anotherKey3: [
value4,
],
anotherKey4: [
value5,
value6]
},
}
What is the simplest way of achieving this result? Currently I have to iterate over the result of the 1st dictionary initializer call:
let grouped = Dictionary(grouping: Array(source), by: {$0.key1)
var grouped2 = [KeyType1 : [KeyType2? : [ValueType]]]()
for pair in grouped {
if let key = pair.key {
grouped2[key] = Dictionary(grouping: pair.value, by: {$0.key1})
}
}
print(grouped2)
And this gets me exactly the result I want: two-level dictionary of arrays.
But I suspect, there is a simpler way of achieving the same result, without manually interating over every key/value pair.
You can do this by doing a init(grouping:by:) call, followed by a mapValues call, which further maps every "group" into another dictionary. And this dictionary is going to be created by init(grouping:by:) again, using the second grouping key.
i.e.
let grouped = Dictionary(grouping: source, by: firstKey)
.mapValues { Dictionary(grouping: $0, by: secondKey) }
For example, to group a bunch of numbers first by their % 2 values, then by their % 4 values:
let source = [1,2,3,4,5,6,7,8,9,10,11,12,13]
let grouped = Dictionary(grouping: source, by: { $0 % 2 })
.mapValues { Dictionary.init(grouping: $0, by: { $0 % 4 }) }
print(grouped)

Get struct Type from json file schema

I want to get structType from a json File lookin like this.
"$schema": "http://json-schema.org/schema#",
"self": {
"vendor": "",
"name": "",
"format": "",
"commentaireVersion": "",
"version": "1.0.1"
},
.....
I've tried to import the Json file as a data set by creating case classes for each field, it didn't work, because i've to create a generic application that can read any other json and get its corresponding struct Type.
maybe should I parse the json to avro ?
Since your JSON has non ASCII characters, you must first remove them to be able to use this solution :
def _decode_list(data):
rv = []
for item in data:
if isinstance(item, unicode):
item = item.encode('ascii', 'ignore')
elif isinstance(item, list):
item = _decode_list(item)
elif isinstance(item, dict):
item = _decode_dict(item)
rv.append(item)
return rv
def _decode_dict(data):
rv = {}
for key, value in data.iteritems():
if isinstance(key, unicode):
key = key.encode('ascii', 'ignore')
if isinstance(value, unicode):
value = value.encode('ascii', 'ignore')
elif isinstance(value, list):
value = _decode_list(value)
elif isinstance(value, dict):
value = _decode_dict(value)
rv[key] = value
return rv
with open('my_json.json', 'r') as f:
json_dict = json.load(f, object_hook=_decode_dict)
Now that you only have UTF-8 characters, you can extract the StructType like this:
rdd_JSON = sc.parallelize([json_dict])
df_JSON = spark.read.json(rdd_JSON)
schema = df_JSON.schema
df_JSON.printSchema()
Your resulting schema :
StructType(List(StructField($metadata,StructType(List(StructField($dataVector,StringType,true),StructField($dataset,StringType,true),StructField($datasource,StringType,true),StructField($fileFormat,StringType,true),StructField($ingestionMode,StringType,true),StructField($nameFormat,StringType,true))),true),StructField($schema,StringType,true),StructField(description,StringType,true),StructField(id,StringType,true),StructField(properties,StructType(List(StructField(content,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(resource,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(accountEtat,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(anonymizationDate,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true),StructField(anonymized,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(ccuId,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true),StructField(comptePrepaye,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(creationDate,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true),StructField(currentSolde,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(solde,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true),StructField(soldeDate,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true),StructField(id,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true),StructField(rechargeCPPEncours,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true),StructField(dateCGV,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true),StructField(dateCreation,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true),StructField(dateDerniereModification,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true),StructField(defaultAddresses,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(payment,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(addressDetail,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(addressL4ExtVoie,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(addressL4LibVoie,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(addressL4MotVoie,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(addressL4NumVoie,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true),StructField(addressName,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(appartment,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(building,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(ceaL4,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(codeInseeCommune,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(country,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(isoCode,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(name,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true),StructField(doorCode1,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(doorCode2,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(mascadiaError,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(poBox,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(postalCode,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(quartierLettre,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(service,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(soColissimoDeliveryMode,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(streetName,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(streetNumber,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(typeVoie,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true),StructField(contact,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(cellPhone,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(company,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(email,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(firstName,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(gender,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true),StructField(lastName,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(phone1,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(phone2,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(title,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true),StructField(shipping,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(addressDetail,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(addressL4ExtVoie,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(addressL4LibVoie,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(addressL4MotVoie,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(addressL4NumVoie,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true),StructField(addressName,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(appartment,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(building,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(ceaL4,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(codeInseeCommune,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(country,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(isoCode,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(name,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true),StructField(doorCode1,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(doorCode2,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(mascadiaError,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(poBox,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(postalCode,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(quartierLettre,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(service,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(soColissimoDeliveryMode,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(streetName,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(streetNumber,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(typeVoie,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true),StructField(contact,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(cellPhone,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(company,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(email,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(firstName,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(gender,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true),StructField(lastName,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(phone1,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(phone2,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(title,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true),StructField(etat,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(fraude,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(dateFraudeNiv1,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true),StructField(dateFraudeNiv2,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true),StructField(dateStatutFraude,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true),StructField(statusFraude,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true),StructField(guestFlg,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(id,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true),StructField(idGuest,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(identity,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(civility,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(dateOfBirth,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true),StructField(firstName,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(lastName,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(mail,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(middleName,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(phone,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true),StructField(isComptePrepaye,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(langage,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(listOfInterests,StructType(List(StructField(description,StringType,true),StructField(items,StructType(List(StructField(properties,StructType(List(StructField(description,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(name,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(uid,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true),StructField(type,StringType,true))),true),StructField(marketing,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(codePromoParrain,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(isFilleul,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true),StructField(nbConso,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true),StructField(optins,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(infosGroupe,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(infosPartners,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(infosPhilaposte,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(infosSmsGroupe,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(infosSmsPartners,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true),StructField(organization,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(codeCoclico,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(codeTypePorteFeuille,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(dateEcheanceDocCertif,StructType(List(StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true),StructField(enseigne,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(function,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(nomSociete,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(numCartePro,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(secteurActivite,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(siret,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(tvaIntraCommunautaire,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(typeEntreprise,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true),StructField(philatelist,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(abonnementCataloguePhilatelique,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(abonnementCatalogue,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(abonnementSortie,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true),StructField(appetencePhilatelie,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(emailingPhilatelistFlg,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(philateListFlg,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true),StructField(termsOfUse,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(testSondeFlg,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(type,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true),StructField(schema,StructType(List(StructField(description,StringType,true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true),StructField(context,StructType(List(StructField(properties,StructType(List(StructField(dateSentEvent,StructType(List(StructField($metadata,StructType(List(StructField($dataKey,StringType,true),StructField($privacyLevel,StringType,true))),true),StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true))),true),StructField(required,ArrayType(StringType,true),true),StructField(type,StringType,true))),true),StructField(messageId,StructType(List(StructField($metadata,StructType(List(StructField($dataKey,StringType,true),StructField($privacyLevel,StringType,true))),true),StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(operation,StructType(List(StructField(description,StringType,true),StructField(properties,StructType(List(StructField(actionDate,StructType(List(StructField($metadata,StructType(List(StructField($dataKey,StringType,true),StructField($privacyLevel,StringType,true))),true),StructField(description,StringType,true),StructField(format,StringType,true),StructField(type,StringType,true))),true),StructField(operationType,StructType(List(StructField($metadata,StructType(List(StructField($dataKey,StringType,true),StructField($privacyLevel,StringType,true))),true),StructField(description,StringType,true),StructField(enum,ArrayType(StringType,true),true),StructField(type,StringType,true))),true),StructField(patch,StructType(List(StructField(description,StringType,true),StructField(items,StructType(List(StructField($metadata,StructType(List(StructField(PATCH_RESOURCE_ID,StringType,true),StructField(PATCH_TARGET_SCHEMA,StringType,true))),true),StructField(properties,StructType(List(StructField(_corrupt_recordvalue,StructType(List(StructField($metadata,StructType(List(StructField($dataKey,StringType,true),StructField($privacyLevel,StringType,true))),true),StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(op,StructType(List(StructField($metadata,StructType(List(StructField($dataKey,StringType,true),StructField($privacyLevel,StringType,true))),true),StructField(description,StringType,true),StructField(enum,ArrayType(StringType,true),true),StructField(type,StringType,true))),true),StructField(path,StructType(List(StructField($metadata,StructType(List(StructField($dataKey,StringType,true),StructField($privacyLevel,StringType,true))),true),StructField(description,StringType,true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true),StructField(type,StringType,true))),true))),true),StructField(type,StringType,true))),true),StructField(resourceId,StructType(List(StructField($metadata,StructType(List(StructField($dataKey,StringType,true),StructField($privacyLevel,StringType,true),StructField($tags,ArrayType(StructType(List(StructField(rdf:type,StringType,true),StructField(rdfs:domain,StringType,true))),true),true))),true),StructField(description,StringType,true),StructField(type,StringType,true))),true),StructField(resourceType,StructType(List(StructField($metadata,StructType(List(StructField($dataKey,StringType,true),StructField($privacyLevel,StringType,true))),true),StructField(description,StringType,true),StructField(type,StringType,true))),true))),true),StructField(self,StructType(List(StructField(commentaireVersion,StringType,true),StructField(format,StringType,true),StructField(name,StringType,true),StructField(vendor,StringType,true),StructField(version,StringType,true))),true),StructField(title,StringType,true),StructField(type,StringType,true)))

Validate when array contains specific value

I want to use an rule like, but I'm having some issues to fullfill the "is" rule for the when condition on Joi validation library.
let schema = {
field1: Joi.array().items(Joi.string().valid('v1', 'v2')),
field2: Joi.when("field1", {
is: // if field1 contains at least 'v1',
then: Joi.object().keys(...),
otherwise: Joi.forbidden()
}
}
You can use array.items by listing all allowed types. If a given type is .required() then there must be a matching item in the array:
joi API reference
let schema = {
field1: Joi.array().items(Joi.string().valid('v1', 'v2')),
field2: Joi.when("field1", {
is: Joi.array().items(Joi.string().valid('v1').required(), Joi.string().valid('v2'))
then: Joi.object().keys(...),
otherwise: Joi.forbidden()
}
}