How to Parse Key-value Pairs from a Base64-encoded String#eg11

We have a Base64-encoded string as follows:

eyJhIjo2MywiYyI6Mjk4LCJuIjoxLCJzIjoxLCJlIjo0MCwicCI6NH0=

And are trying to parse the transcoded JSON string ({“a”:63,”c”:298,”n”:1,”s”:1,”e”:40,”p”:4}) as a table. Below is the desired result:

SQL written…


This content originally appeared on DEV Community and was authored by Judy

We have a Base64-encoded string as follows:

eyJhIjo2MywiYyI6Mjk4LCJuIjoxLCJzIjoxLCJlIjo0MCwicCI6NH0=

And are trying to parse the transcoded JSON string ({"a":63,"c":298,"n":1,"s":1,"e":40,"p":4}) as a table. Below is the desired result:

Image description
SQL written in MySQL:

SELECT

            JSON_EXTRACT(CONVERT(FROM_BASE64('eyJhIjo2MywiYyI6Mjk4LCJuIjoxLCJzIjoxLCJlIjo0MCwicCI6NH0=')

                        USING utf8),

            '$."a"') a,

            JSON_EXTRACT(CONVERT(FROM_BASE64('eyJhIjo2MywiYyI6Mjk4LCJuIjoxLCJzIjoxLCJlIjo0MCwicCI6NH0=')

                        USING utf8),

            '$."c"') c,

            JSON_EXTRACT(CONVERT(FROM_BASE64('eyJhIjo2MywiYyI6Mjk4LCJuIjoxLCJzIjoxLCJlIjo0MCwicCI6NH0=')

                        USING utf8),

            '$."n"') n,

            JSON_EXTRACT(CONVERT(FROM_BASE64('eyJhIjo2MywiYyI6Mjk4LCJuIjoxLCJzIjoxLCJlIjo0MCwicCI6NH0=')

                        USING utf8),

            '$."s"') s,

            JSON_EXTRACT(CONVERT(FROM_BASE64('eyJhIjo2MywiYyI6Mjk4LCJuIjoxLCJzIjoxLCJlIjo0MCwicCI6NH0=')

                        USING utf8),

            '$."e"') e,

            JSON_EXTRACT(CONVERT(FROM_BASE64('eyJhIjo2MywiYyI6Mjk4LCJuIjoxLCJzIjoxLCJlIjo0MCwicCI6NH0=')

                        USING utf8),

            '$."p"') p

There is nothing hard about it. We only need to convert the Base64-encoded strings into JSON strings, then parse JSON into a table according to K-V pairs. SQL coding is complicated, particularly when the number of columns in the result table is unknown.

 

It is easy to code it in the open-source esProc SPL:

Suppose the value of parameter arg1 is:

eyJhIjo2MywiYyI6Mjk4LCJuIjoxLCJzIjoxLCJlIjo0MCwicCI6NH0=

Image description
As the open-source, professional structured data computation language, SPL is convenient in handling various data sources, including JSON.


This content originally appeared on DEV Community and was authored by Judy


Print Share Comment Cite Upload Translate Updates
APA

Judy | Sciencx (2024-07-15T06:38:38+00:00) How to Parse Key-value Pairs from a Base64-encoded String#eg11. Retrieved from https://www.scien.cx/2024/07/15/how-to-parse-key-value-pairs-from-a-base64-encoded-stringeg11/

MLA
" » How to Parse Key-value Pairs from a Base64-encoded String#eg11." Judy | Sciencx - Monday July 15, 2024, https://www.scien.cx/2024/07/15/how-to-parse-key-value-pairs-from-a-base64-encoded-stringeg11/
HARVARD
Judy | Sciencx Monday July 15, 2024 » How to Parse Key-value Pairs from a Base64-encoded String#eg11., viewed ,<https://www.scien.cx/2024/07/15/how-to-parse-key-value-pairs-from-a-base64-encoded-stringeg11/>
VANCOUVER
Judy | Sciencx - » How to Parse Key-value Pairs from a Base64-encoded String#eg11. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/15/how-to-parse-key-value-pairs-from-a-base64-encoded-stringeg11/
CHICAGO
" » How to Parse Key-value Pairs from a Base64-encoded String#eg11." Judy | Sciencx - Accessed . https://www.scien.cx/2024/07/15/how-to-parse-key-value-pairs-from-a-base64-encoded-stringeg11/
IEEE
" » How to Parse Key-value Pairs from a Base64-encoded String#eg11." Judy | Sciencx [Online]. Available: https://www.scien.cx/2024/07/15/how-to-parse-key-value-pairs-from-a-base64-encoded-stringeg11/. [Accessed: ]
rf:citation
» How to Parse Key-value Pairs from a Base64-encoded String#eg11 | Judy | Sciencx | https://www.scien.cx/2024/07/15/how-to-parse-key-value-pairs-from-a-base64-encoded-stringeg11/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.