A
- AccessControlEntry.ConditionToIdentities
- Access.Database
- ActiveDirectory.Domains
- AdobeAnalytics.Cubes
- AdoDotNet.DataSource
- AdoDotNet.Query
- AnalysisServices.Database
- AnalysisServices.Databases
- AzureStorage.BlobContents
- AzureStorage.Blobs
- AzureStorage.DataLake
- AzureStorage.DataLakeContents
- AzureStorage.Tables
B
- BinaryFormat.Binary
- BinaryFormat.Byte
- BinaryFormat.ByteOrder
- BinaryFormat.Choice
- BinaryFormat.Decimal
- BinaryFormat.Double
- BinaryFormat.Group
- BinaryFormat.Length
- BinaryFormat.List
- BinaryFormat.Null
- BinaryFormat.Record
- BinaryFormat.SignedInteger16
- BinaryFormat.SignedInteger32
- BinaryFormat.SignedInteger64
- BinaryFormat.Single
- BinaryFormat.Text
- BinaryFormat.Transform
- BinaryFormat.UnsignedInteger16
- BinaryFormat.UnsignedInteger32
- BinaryFormat.UnsignedInteger64
- BinaryFormat.7BitEncodedSignedInteger
- BinaryFormat.7BitEncodedUnsignedInteger
- Binary data
- Binary.ApproximateLength
- Binary.Buffer
- Binary.Combine
- Binary.Compress
- Binary.Decompress
- Binary.From
- Binary.FromList
- Binary.FromText
- Binary.InferContentType
- Binary.Length
- Binary.Range
- Binary.Split
- Binary.ToList
- Binary.ToText
- Binary.View
- Binary.ViewError
- Binary.ViewFunction
- Byte.From
C
- Cdm.Contents
- Character.FromNumber
- Character.ToNumber
- Combiner.CombineTextByDelimiter
- Combiner.CombineTextByEachDelimiter
- Combiner.CombineTextByLengths
- Combiner.CombineTextByPositions
- Combiner.CombineTextByRanges
- Comparer.FromCulture
- Comparer.Ordinal
- Comparer.OrdinalIgnoreCase
- Controlling byte order
- Csv.Document
- Cube.AddAndExpandDimensionColumn
- Cube.AddMeasureColumn
- Cube.ApplyParameter
- Cube.AttributeMemberId
- Cube.AttributeMemberProperty
- Cube.CollapseAndRemoveColumns
- Cube.Dimensions
- Cube.DisplayFolders
- Cube.MeasureProperties
- Cube.MeasureProperty
- Cube.Measures
- Cube.Parameters
- Cube.Properties
- Cube.PropertyKey
- Cube.ReplaceDimensions
- Cube.Transform
- Currency.From
D
- DateTime.AddZone
- DateTime.Date
- DateTime.FixedLocalNow
- DateTime.From
- DateTime.FromFileTime
- DateTime.FromText
- DateTime.IsInCurrentHour
- DateTime.IsInCurrentMinute
- DateTime.IsInCurrentSecond
- DateTime.IsInNextHour
- DateTime.IsInNextMinute
- DateTime.IsInNextNHours
- DateTime.IsInNextNMinutes
- DateTime.IsInNextNSeconds
- DateTime.IsInNextSecond
- DateTime.IsInPreviousHour
- DateTime.IsInPreviousMinute
- DateTime.IsInPreviousNHours
- DateTime.IsInPreviousNMinutes
- DateTime.IsInPreviousNSeconds
- DateTime.IsInPreviousSecond
- DateTime.LocalNow
- DateTime.Time
- DateTime.ToRecord
- DateTime.ToText
- Date.AddDays
- Date.AddMonths
- Date.AddQuarters
- Date.AddWeeks
- Date.AddYears
- Date.Day
- Date.DayOfWeek
- Date.DayOfWeekName
- Date.DayOfYear
- Date.DaysInMonth
- Date.EndOfDay
- Date.EndOfMonth
- Date.EndOfQuarter
- Date.EndOfWeek
- Date.EndOfYear
- Date.From
- Date.FromText
- Date.IsInCurrentDay
- Date.IsInCurrentMonth
- Date.IsInCurrentQuarter
- Date.IsInCurrentWeek
- Date.IsInCurrentYear
- Date.IsInNextDay
- Date.IsInNextMonth
- Date.IsInNextNDays
- Date.IsInNextNMonths
- Date.IsInNextNQuarters
- Date.IsInNextNWeeks
- Date.IsInNextNYears
- Date.IsInNextQuarter
- Date.IsInNextWeek
- Date.IsInNextYear
- Date.IsInPreviousDay
- Date.IsInPreviousMonth
- Date.IsInPreviousNDays
- Date.IsInPreviousNMonths
- Date.IsInPreviousNQuarters
- Date.IsInPreviousNWeeks
- Date.IsInPreviousNYears
- Date.IsInPreviousQuarter
- Date.IsInPreviousWeek
- Date.IsInPreviousYear
- Date.IsInYearToDate
- Date.IsLeapYear
- Date.Month
- Date.MonthName
- Date.QuarterOfYear
- Date.StartOfDay
- Date.StartOfMonth
- Date.StartOfQuarter
- Date.StartOfWeek
- Date.StartOfYear
- Date.ToRecord
- Date.ToText
- Date.WeekOfMonth
- Date.WeekOfYear
- Date.Year
- DB2.Database
- Decimal.From
- Diagnostics.ActivityId
- Diagnostics.Trace
- DirectQueryCapabilities.From
- Double.From
- Duration.Days
- Duration.From
- Duration.FromText
- Duration.Hours
- Duration.Minutes
- Duration.Seconds
- Duration.ToRecord
- Duration.TotalDays
- Duration.TotalHours
- Duration.TotalMinutes
- Duration.TotalSeconds
- Duration.ToText
E
F
G
H
I
L
- Lines.FromBinary
- Lines.FromText
- Lines.ToBinary
- Lines.ToText
- List.Accumulate
- List.AllTrue
- List.Alternate
- List.AnyTrue
- List.Average
- List.Buffer
- List.Combine
- List.ConformToPageReader
- List.Contains
- List.ContainsAll
- List.ContainsAny
- List.Count
- List.Covariance
- List.Dates
- List.DateTimes
- List.DateTimeZones
- List.Difference
- List.Distinct
- List.Durations
- List.FindText
- List.First
- List.FirstN
- List.Generate
- List.InsertRange
- List.Intersect
- List.IsDistinct
- List.IsEmpty
- List.Last
- List.LastN
- List.MatchesAll
- List.MatchesAny
- List.Max
- List.MaxN
- List.Median
- List.Min
- List.MinN
- List.Mode
- List.Modes
- List.NonNullCount
- List.Numbers
- List.Percentile
- List.PositionOf
- List.PositionOfAny
- List.Positions
- List.Product
- List.Random
- List.Range
- List.RemoveFirstN
- List.RemoveItems
- List.RemoveLastN
- List.RemoveMatchingItems
- List.RemoveNulls
- List.RemoveRange
- List.Repeat
- List.ReplaceMatchingItems
- List.ReplaceRange
- List.ReplaceValue
- List.Reverse
- List.Select
- List.Single
- List.SingleOrDefault
- List.Skip
- List.Sort
- List.Split
- List.StandardDeviation
- List.Sum
- List.Times
- List.Transform
- List.TransformMany
- List.Union
- List.Zip
- Logical.From
- Logical.FromText
Understanding JSON
Before we dive into the M code behind Json.FromValue, let’s first understand what JSON is and why it’s important. JSON, or JavaScript Object Notation, is a lightweight text-based format for representing data. It’s easy for humans to read and write, and it’s also easy for machines to parse and generate. JSON is commonly used for web-based data exchange and storage, and it’s also used for configuration files and other data that needs to be easily readable and writable.
JSON is essentially a collection of key-value pairs, where the keys are strings and the values can be any valid JSON data type, including strings, numbers, booleans, nulls, arrays, and objects. Here’s an example of a simple JSON document:
{
“name”: “John Doe”,
“age”: 30,
“isMarried”: false,
“hobbies”: [“reading”, “cooking”, “traveling”],
“address”: {
“street”: “123 Main St”,
“city”: “New York”,
“state”: “NY”,
“zip”: “10001”
}
}
As you can see, the document contains five key-value pairs, where the values include a string, a number, a boolean, an array, and an object.
The Json.FromValue Function
The Json.FromValue function in Power Query M converts a value into a JSON document. The value can be any valid Power Query M expression, including a record, a list, a table, or a primitive value such as a number or a string.
Here's the syntax for the Json.FromValue function:
Json.FromValue(value as any, optional options as nullable record) as text
The function takes two arguments: the value to be converted and an optional record of options. The value can be any valid Power Query M expression, and the optional options record can be used to specify options such as the indentation level and whether to include null values.
Here's an example of using the Json.FromValue function to convert a record into a JSON document:
let
myRecord = [name=”John Doe”, age=30, isMarried=false],
myJson = Json.FromValue(myRecord)
in
myJson
The resulting JSON document will look like this:
{
“name”: “John Doe”,
“age”: 30,
“isMarried”: false
}
As you can see, the Json.FromValue function has converted the record into a JSON document with three key-value pairs.
Understanding the M Code
Now that we understand what JSON is and how the Json.FromValue function works, let's take a closer look at the M code behind the function.
The M code for the Json.FromValue function is relatively simple. Here's what it looks like:
(value as any, optional options as nullable record) =>
let
indent = if Record.HasFields(options, “Indent”) then options[Indent] else 0,
includeNulls = if Record.HasFields(options, “IncludeNulls”) then options[IncludeNulls] else false,
json = Json.Document(Json.FromValue(value, null)),
formattedJson = if indent = 0 then json else Json.Transform(json, {{“”, each Text.PadStart(“”, indent [Level], ” “) & _}})
in
if includeNulls then Text.Replace(formattedJson, “null”, “null”) else Text.Replace(formattedJson, “, null”, “”)
The function takes the value and options arguments, and then performs the following steps:
1. It checks the options record for the "Indent" field, and if it's present, sets the indent variable to the specified value. If the "Indent" field is not present, the indent variable is set to 0.
2. It checks the options record for the "IncludeNulls" field, and if it's present, sets the includeNulls variable to the specified value. If the "IncludeNulls" field is not present, the includeNulls variable is set to false.
3. It uses the Json.FromValue function to convert the value into a JSON document.
4. It formats the JSON document using the Text.PadStart function to add indentation based on the specified indent level.
5. It replaces any null values with either the string "null" or an empty string, depending on the value of the includeNulls variable.
6. It returns the formatted JSON document as text.
In conclusion, the Json.FromValue function in Power Query M is a powerful tool for converting data into the JSON format. The M code behind the function is relatively simple, but it provides a great deal of flexibility and control over the resulting JSON document. By understanding the M code behind the function, you can more effectively use the function in your data transformation and analysis tasks, and you can also customize the function to meet your specific needs.