Android Question [Solved] Json 2 Csv

FrankDev

Active Member
Licensed User
Longtime User
Hello

i have a very simple json structure here.
What is the easiest way to convert it to CSV format ?

probably there is a clever solution

B4X:
{
    "root": [
        {
            "customerID": "12874",
            "articleID": 12,
            "ordernumber": "25503",
            "articlename": "Wein 1",
            "quantity": 5,
            "price": 2.44
        },
        {
            "customerID": "12874",
            "articleID": 20,
            "ordernumber": "82725",
            "articlename": "Wein 2",
            "quantity": 5,
            "price": 12.10
        },
        {
            "customerID": "12874",
            "articleID": 25,
            "ordernumber": "7414",
            "articlename": "Wein 2",
            "quantity": 5,
            "price": 2.19
        }
    ]
}

Best regards
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
What is the easiest way to convert it to CSV format ?
Parse the json (jsontool), create the csv to save by yourself.
It is trivial to do it
 
Upvote 0

FrankDev

Active Member
Licensed User
Longtime User
Hello,

i am looking for a function that i pass such a json file to and that saves it as a csv. The structure is always similar to the given one, only there can be more fields.

If you are interested to do this please send me a short PN.
Of course this is paid
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
attached is an old json2csv class updated based

Dr Got Jr,:
You class works well as I tested the OP json. text. I only had to correct a small typo in the class: MsbGox should be: Msgbox
Also, the format should be: {root:[]}. Here is the complete project:
B4X:
Sub Globals
    Private CCSV As CleverCSV
End Sub

Sub Activity_Create(FirstTime As Boolean)
    CCSV.Initialize
    Dim txt As String = $"{
    "root": [
        {
            "customerID": "12874",
            "articleID": 12,
            "ordernumber": "25503",
            "articlename": "Wein 1",
            "quantity": 5,
            "price": 2.44
        },
        {
            "customerID": "12874",
            "articleID": 20,
            "ordernumber": "82725",
            "articlename": "Wein 2",
            "quantity": 5,
            "price": 12.10
        },
        {
            "customerID": "12874",
            "articleID": 25,
            "ordernumber": "7414",
            "articlename": "Wein 2",
            "quantity": 5,
            "price": 2.19
        }
    ]
}"$

    Log(CCSV.toCSV(txt))
   
'OUTPUT:
'    customerID,articleID,ordernumber,articlename,quantity,price
'    12874,12,25503,Wein 1,5,2.44
'    12874,20,82725,Wein 2,5,12.1
'    12874,25,7414,Wein 2,5,2.19
 
Last edited:
Upvote 0

FrankDev

Active Member
Licensed User
Longtime User
Hello

I have marked this as 'solved', which can be found in the thread
a solution from 'drgottjr'.
So that someone who is looking for it can see that there is a solution.
Is this the wrong approach?

Greetings
Frank
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I slightly modified the CleverCSV class module to allow having the output with a header row or without: Log(CCSV.toCSV(txt, True)) 'false means has no header
 

Attachments

  • CleverCSV.bas
    2 KB · Views: 175
Upvote 0

FrankDev

Active Member
Licensed User
Longtime User
Hello,

it's not quite working yet.
For example, if the article name contains a comma
the field is assigned incorrectly

(For me, however, this fits so, because I do not use commas, only as a hint)

Best regards

B4X:
Dim txt As String = $"{

    "root": [
{
"customerID": "12874",
"articleID": 12,
"ordernumber": "25503",
"articlename": "Wein 1",
"quantity": 5,
"price": 2.44
},
{
"customerID": "12874",
"articleID": 20,
"ordernumber": "82725",
"articlename": "Wein 2, 1934 Cabernet",
"quantity": 5,
"price": 12.10
},
{
"customerID": "12874",
"articleID": 25,
"ordernumber": "7414",
"articlename": "Wein 2",
"quantity": 5,
"price": 2.19
}
]
}"$
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
To eliminate any issues with commas in some fields and issues having to deal with double quotes , I changed the class so you can pick your own delimiter and whether the string shows a header or not.
B4X:
Log(CCSV.toCSV(txt, True, ";"))   'false means has no header, delimiter wanted
 

Attachments

  • CleverCSV.bas
    2.2 KB · Views: 175
Upvote 0

Mahares

Expert
Licensed User
Longtime User
we seem to be going beyond csv.
Most programs that open these types of files have no problems with comma or semi colon. You do not have to use it or replace the original. It is there for anyone who wants it. The forum is full of developers who have different levels and needs. Posts in any thread are not limited to the OP or one individual.
 
Upvote 0
Top