-
Notifications
You must be signed in to change notification settings - Fork 90
JSON key can have special character #1828
Comments
Hi @brianchu, this is a problem that arrises from the definition of the grammar for Beagle expressions: Grammar
ProblemAny accessed property is identified by In truth, considering we don't currently use the minus operator, we could support hyphen on keys without any issue. But, it wouldn't solve the other scenarios and we'd be preventing us from extending the language to include infix operators in the future. We are currently developing some new server driven libraries to work with SwiftUI and Compose. In these new libraries we'll extend the grammar to include new functionalities, one of them is dynamic state access, which will solve this issue; example: "myState['my-key $*-with-special-characters']". Unfortunately, we can't bring this to Beagle yet, considering it would require a very high amount of work and we have other priorities (like the aforementioned libs). A solution that can be used nowAs you said, one solution is to do a transformation before accessing it. This can be done via a custom operation. First, you should create a custom operation in the frontend. I'm gonna give you an example on React: function sanitizeKey(key: string): string {
// replaces both hyphens and spaces with underscores
return key.replace(/[\s\-]/g, '_')
}
function sanitize(data?: any): any {
if (Array.isArray(data)) {
return data.map(sanitize)
}
if (data != null && typeof data == 'object') {
let result: object = {}
Object.keys(data).forEach(key => {
result[sanitizeKey(key)] = sanitize(data[key])
})
return result
}
return data
}
let beagle = createBeagleUIService({
baseUrl: '',
customOperations: {
sanitize,
},
components: {},
}) Then, in the backend, assuming you're setting a Beagle Context from a sendRequest action, you should use the new operation before setting the context (I'll call it {
"_beagleAction_": "beagle:sendRequest",
"url": "your_url",
"onSuccess": [{
"_beagleAction_": "beagle:setContext",
"contextId": "result",
"value": "@{sanitize(onSuccess.data)}"
}]
} Another solution that can be used nowYou can also create a custom operation called function get(data: any, key: string): any {
return (data != null && typeof data == 'object') ? data[key] : null
}
let beagle = createBeagleUIService({
baseUrl: '',
customOperations: {
get,
},
components: {},
}) Then, in the backend, you can access the data with this custom operation (I'll assume your context is called {
"_beagleComponent_": "beagle:text",
"text": "get(result, 'some-other-key-you-know-can-look-like-this')",
} The only problem with this approach is that, if your data is deeply nested, you'll have a lot of nested calls to |
I'm closing this issue due to inactivity. Please, fell free to reopen this if necessary. |
Use case
Unfortunately, the JSON data I got from the server has structure and naming like this:
{
"data" : {
"some-key": "some value",
"some-other-key-you-know-can-look-like-this": "value can have t-h-i-s thing too",
"okkey": "ok value"
}
}
When I try to access the JSON via saved context
@{data.some-key} it doesn't work.
For other result without the hyphen "-", it work.
Proposal
The text was updated successfully, but these errors were encountered: