B4J Question JSONQuery - Is there a way to pass the string once for multiple queries of the same string?

Mashiane

Expert
Licensed User
Longtime User
Hi

I have a lot of "queries" that I need to perform on the same string and I need to query parts of it and put it on a type.

For example

B4X:
Dim msg As ChatMessage
    msg.Initialize
    msg.size = 0
    'parse the message
    Dim jq As JSONQuery
    jq.Initialize
    '
    msg.chatId = jq.Query(realMsg, "chatId")
    'ignore group message
    If msg.chatId.EndsWith("@g.us") Then Return msg
    'normal message will be 1
    msg.size = 1    
msg.fileName = jq.Query(realMsg,  "messageData.fileMessageData.fileName")
                    msg.mimeType = jq.Query(realMsg,  "messageData.fileMessageData.mimeType")
                    msg.downloadUrl = jq.Query(realMsg,  "messageData.fileMessageData.downloadUrl")
                    msg.caption = jq.Query(realMsg,  "messageData.fileMessageData.caption")
                    msg.jpegThumbnail = jq.Query(realMsg,  "messageData.fileMessageData.jpegThumbnail")

The jsonQuery.Query method is expecting this string all the time, granted. For my use case I need some optimization

I need a way to pass this string just once, for example

You initialize the JSONQuery with the string, one
B4X:
jq.Initialize1(realMsg)

This does whatever is needed to have the string ready for querying I guess.

And then simpler calls like the following to query the needed paths

B4X:
msg.fileName = jq.Query1("messageData.fileMessageData.fileName")
                    msg.mimeType = jq.Query1("messageData.fileMessageData.mimeType")
                    msg.downloadUrl = jq.Query1("messageData.fileMessageData.downloadUrl")
                    msg.caption = jq.Query1("messageData.fileMessageData.caption")
                    msg.jpegThumbnail = jq.Query1("messageData.fileMessageData.jpegThumbnail")

Is this something possible with JSONQuery / can be implemented? Thank you so much in advance.

Another one.

Is it possible to set a value in a path? For example, if I wanted to update the mimeType to a string jpeg, can I do this?

B4X:
jq.SetPath("messageData.fileMessageData.mimeType", "jpeg")

This to update the original string and also be able to return this update as a Map?

Thanks again.
 
Top