about the validity of the json message - json

I am wondering why the following is not a valid json
{ test : event}
but
{ "test" : "event" }
is a valid one?

Because in JSON, fields are declared in quotes (so it's "test" and not test), and the only data types a field can store are: number, string, boolean, array, object (that is, another JSON object), or null. event isn't any of those things, but "event" is a string, which is a valid value type. Those are just the rules of JSON.
You should read up on JSON.

Related

Elasticsearch 8.0.0 mapper_parsing_exception of a string literal for field type "flattened"

I have a Problem to insert a document via Api to my ES 8.0.0.
In my IndexTemplate I defined a mapping of a property called [Data] of type "flattend".
For "normal" JSON-Objects it works fine. But when I try to insert a plain string literal (for example "test") or a number (for example 4) I get a "400 Bad Request". JSONLint says it's a valid JSON!!
{
....
"Data": "test",
....
}
Can i configure ES to accept such kind of JSON for type "flattened"??
As Elasticsearch document mentions:
The flattened type provides an alternative approach, where the entire
object is mapped as a single field. Given an object, the flattened
mapping will parse out its leaf values and index them into one field
as keywords.
So, the value provided for the "flattened" field type should be a JsonObject.
Hence, below works as where "full_name" is of type "flattened"
"full_name":{
"name":"nishikant"
}
But below does not
"full_name":"nishikant".
Same has been given in exception
"reason" : "Failed to parse object: expecting token of type [START_OBJECT] but found [VALUE_STRING]"

Dynamic tag in JSON

I am generating one JSON file and I have a variable of the below type in TypeScript:
export interface Rules{
name: string,
condition: string,
}
I want to parse the 'Rule' type in JSON in the below format:
"dataRetentionRules": {
"RULE_1": {
"name": "test123",
"condition": "RetentionStartDate"
},
"RULE_2": {
"name": "test456",
"condition": "RetentionEndDate"
}
}
How can I make this rule tag dynamic? I mean how do I parse this "RULE_1", "RULE_2" tag? Since tags are supposed to be constant I believe. How can I achieve the above format?
You can make use of the typescript Record built-in type. Behind the scenes you are using the principles of index types and mapped types.
But all that you really need to know it this: Record<string, Rules> defines an object whose keys are strings and whose values are instances of Rules.
Your JSON object can be described by
interface HasRetentionRules {
dataRetentionRules: Record<string, Rules>
}
Note that this is asserting that any string key corresponds to a Rules object and it does not check for invalid keys, so you need to check for invalid keys yourself. Alternatively, you can use Partial<Record<string, Rules>> which states that any string key has a value which is of type Rules or undefined.
Typescript Playground Link

XML to json with transform

I have the following XML:
<ArrayOfAnyType xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<anyType xsi:type="xsd:dateTime">2016-09-14T13:58:30Z</anyType>
<anyType xsi:type="xsd:decimal">1.2</anyType>
</ArrayOfAnyType>
And I am unmarshaling it to the following structs:
type Value struct {
Data []Data `xml:"anyType"`
}
type Data struct {
Key string `xml:"http://www.w3.org/2001/XMLSchema-instance type,attr"`
Value string `xml:",chardata"`
}
I would ultimately like to marshal them to json of this structure:
{
timestamp: [the value of xsi:type="xsd:dateTime"]
value: [the value of xsi:type="xsd:decimal"]
}
I'm new to Go, so I'm not sure if this is even possible.
Field tags in Go allow specifying any number of distinct values — separated by spaces.
So in your simple case just add json:"value" after xml:"..." and then marshal
the unmarshaled value to JSON.
Note that I mean spaces inside a single tag, not a space-separated tags:
Field string `xml:"myns Xyzzy" json:"Blorb"`
In more complex cases it might be needed to have distinct types for unmarshaling
and marshaling, and shovel data with required intermediate conversions
between the values of these types.

Merging dynamic field with type date/string triggered conflict

I'm uploading json files on my Elasticsearch server and I have an object "meta" with a field name and a field value. Sometimes value is a string and sometimes is a date so the dynamic mapping doesn't work.
I tried to put an explicit mapping to set the field to string but I always have the same error "Merging dynamic updates triggered a conflict: mapper [customer.meta.value] of different type, current_type [string], merged_type [date]"}}}, :level=>:warn"
Can I use the parameter "ignore_conflict" or how can I upload multi type field?
Thx
You cannot have two data types for same field in elasticsearch. It is not possible to index it. Dynamic mapping means that the type is identified from the first value that is inserted into the field. If you try to insert some other type in that field, it'll be an error. If you need to store both string and date, your best bet is to set the mapping to use string and explicitly convert your dates to string before passing it to elasticsearch.
I disabled date_detection for _ default_ and that's working.
Now my problem is the following: I want to disable date_detection only for meta.value and customer.meta.value. It's correct for the first but I can't for the second because it's an nested object I think.
I tried this:
curl -XPUT 'localhost:9200/rr_sa' -d '
{
"mappings": {
"meta": {
"date_detection": false
},
"customer.meta": {
"date_detection": false
}
}
}
'

Key Value Pair Datatype in FB 4.6

I have a JSON response form HTTP request.
"AdditionalData": {
"default" : "checked",
"example" : "empty"
}
This specific response would ideally be interoperated as a dictionary type (Key value pair). But when I auto-detect the return type of the JSON, FB 4.6 makes it of type Object. This does not work for me. For some reason the AdditionalData object in the model that I'm mapping is always null. What data type can I manually set this response to?
I don't know if you can force the result of a complex grouping to be anything but an Object when it gets returned. Once it arrives you could convert it into an ArrayCollection (of Objects or other ArrayCollections) though.