diff --git a/src/interface.jl b/src/interface.jl index ec43801..8d647bd 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -6,7 +6,7 @@ export noNegative!, randomWithProb, randomChoiceWithProb, findIndex, limitvalue, matMul_3Dto4D_batchwise, isNotEqual, linearToCartesian, vectorMax, multiply_last, multiplyRandomElements, replaceElements, replaceElements!, isBetween, isLess, allTrue, getStringBetweenCharacters, JSON3read_stringKey, mkDictPath!, - getDictPath, dataframeToCSV + getDictPath, dataframeToCSV, dfToJSONRows using JSON3, DataStructures, Distributions, Random, Dates, UUIDs, MQTTClient, DataFrames, CSV using ..util, ..communication @@ -1174,6 +1174,89 @@ function dataframeToCSV(df::DataFrame) dfStr = String(take!(io)) return dfStr end + +""" Convert a DataFrame into a list of JSON rows. + +# Arguments + - `df::DataFrame` + The input DataFrame to be converted. + +# Return + - `rows::Vector{Dict{String, Any}}` + A vector of dictionaries, where each dictionary represents a row in JSON format. + +# Example + ```jldoctest + julia> using DataFrame, JSON3 + julia> df = DataFrame(A = [1, 2, 3], B = ["apple", "banana", "cherry"]) + julia> json_rows = dfToJSONRows(df) + ``` + +# Signature +""" +function dfToJSONRows(df::DataFrame) + rows = [] + for row in eachrow(df) + json_row = Dict{String, Any}() + for col in names(df) + json_row[col] = row[col] + end + push!(rows, json_row) + end + return rows +end + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +