Forum Discussion
Power BI - Deneb visual - Read a JSON to create the visuals
- 8 months ago
JSON.parse() is not a valid expression in Vega. For security reasons, you don't have access to JavaScript in Vega, and they provide many expressions that solve a lot of common scenarios. Even if Vega did have access to JS through its runtime, we would not be able to execute it in a certified visual, as MS certification rules prohibit it. In addition to it being a powerful language, it's one of the reasons I chose Vega for this visual, as their parser solves many challenges that the certification rules impose without me having to solve them myself.
Right now, I don't think that what you're attempting will be possible. If you can add a column that can potentially work in a comma-separated format, you might be able to do something with that (e.g., split() ) in a transform to turn it into an array of values, but you won't be able to parse a JSON string into an object.
Regards,
Daniel
The Deneb visual is sandboxed. You will not be able to access any external data from it. Your JSON would have to be stored in a column in your data and handed over to the visual as a datum.
Hi Ibendlin.
Forget the problem about Dynamic data source (I updated my first message for not creating more confusion).I found where come from the problem and it is not linked to my deneb project ..was a test table I didn't remove.
But the question is still completely valid.
I have a table in my Power BI with this JSON (provided as example) in a text column.
I was able to provide this column to deneb and in the deneb debugger I can see those data. But when I use JSON.parse I have lot of different errrors. So, in other words I don't know how to use the JSON.parser in deneb. I'm using Vega 6.2.0 included in Power BI
So if you have an idea ??
- lbendlin8 months agoSuper User
But when I use JSON.parse I have lot of different errrors.What are these?
I would recommend you start with a much smaller sample. Single column, single JSON value etc. Then try to parse that and render in your visual.
If you provide a small proof of concept it will be easier to help.
- DGPBi8 months agoHelper I
Hi Ibendlin,
Thanks for your answer.
Please find here a small code with an embedded example. I still have : [Error] Illegal callee type: MemberExpression as result and I don't see mymy_json_column in data{ "$schema": "https://vega.github.io/schema/vega/v5.json", "description": "A minimal Vega example demonstrating how to parse a JSON string and create a simple chart.", "width": 500, "height": 200, "padding": 5, "data": [ { "name": "source", "values": [ { "my_json_column": "{\"schedule_rules\":[{\"day_num\":\"0\",\"segments\":[{\"end_time\":\"23:59\",\"start_time\":\"00:00\",\"type\":\"No Production\"}]},{\"day_num\":\"1\",\"segments\":[{\"end_time\":\"12:30\",\"start_time\":\"08:30\",\"type\":\"Normal\"},{\"end_time\":\"16:30\",\"start_time\":\"13:30\",\"type\":\"Normal\"}]},{\"day_num\":\"2\",\"segments\":[{\"end_time\":\"12:30\",\"start_time\":\"08:30\",\"type\":\"Normal\"},{\"end_time\":\"16:30\",\"start_time\":\"13:30\",\"type\":\"Normal\"}]},{\"day_num\":\"3\",\"segments\":[{\"end_time\":\"12:30\",\"start_time\":\"08:30\",\"type\":\"Normal\"},{\"end_time\":\"16:30\",\"start_time\":\"13:30\",\"type\":\"Normal\"}]},{\"day_num\":\"4\",\"segments\":[{\"end_time\":\"12:30\",\"start_time\":\"08:30\",\"type\":\"Normal\"},{\"end_time\":\"16:30\",\"start_time\":\"13:30\",\"type\":\"Normal\"}]},{\"day_num\":\"5\",\"segments\":[{\"end_time\":\"12:30\",\"start_time\":\"08:30\",\"type\":\"Normal\"},{\"end_time\":\"16:30\",\"start_time\":\"13:30\",\"type\":\"Normal\"}]},{\"day_num\":\"6\",\"segments\":[{\"end_time\":\"23:59\",\"start_time\":\"00:00\",\"type\":\"No Production\"}]}]}" } ], "transform": [ { "type": "formula", "as": "parsed_json", "expr": "JSON.parse(datum.my_json_column)" }, { "type": "flatten", "fields": ["parsed_json.schedule_rules"], "as": ["rule"] }, { "type": "flatten", "fields": ["rule.segments"], "as": ["segment"] }, { "type": "formula", "as": "startTime", "expr": "toNumber(split(datum.segment.start_time, ':')[0]) + (toNumber(split(datum.segment.start_time, ':')[1]) / 60)" }, { "type": "formula", "as": "endTime", "expr": "toNumber(split(datum.segment.end_time, ':')[0]) + (toNumber(split(datum.segment.end_time, ':')[1]) / 60)" }, { "type": "formula", "as": "day", "expr": "datum.rule.day_num" }, { "type": "formula", "as": "type", "expr": "datum.segment.type" } ] } ], "scales": [ { "name": "yscale", "type": "band", "range": "height", "domain": {"data": "source", "field": "day"}, "padding": 0.1 }, { "name": "xscale", "type": "linear", "range": "width", "domain": [0, 24] }, { "name": "color", "type": "ordinal", "range": "category", "domain": {"data": "source", "field": "type"} } ], "axes": [ {"orient": "bottom", "scale": "xscale", "tickCount": 24, "title": "Hour of Day"}, {"orient": "left", "scale": "yscale", "title": "Day of Week"} ], "legends": [ {"fill": "color", "title": "Segment Type"} ], "marks": [ { "type": "rect", "from": {"data": "source"}, "encode": { "enter": { "y": {"scale": "yscale", "field": "day"}, "height": {"scale": "yscale", "band": 1}, "x": {"scale": "xscale", "field": "startTime"}, "x2": {"scale": "xscale", "field": "endTime"}, "fill": {"scale": "color", "field": "type"} } } } ] }- dm-p8 months agoSuper User
JSON.parse() is not a valid expression in Vega. For security reasons, you don't have access to JavaScript in Vega, and they provide many expressions that solve a lot of common scenarios. Even if Vega did have access to JS through its runtime, we would not be able to execute it in a certified visual, as MS certification rules prohibit it. In addition to it being a powerful language, it's one of the reasons I chose Vega for this visual, as their parser solves many challenges that the certification rules impose without me having to solve them myself.
Right now, I don't think that what you're attempting will be possible. If you can add a column that can potentially work in a comma-separated format, you might be able to do something with that (e.g., split() ) in a transform to turn it into an array of values, but you won't be able to parse a JSON string into an object.
Regards,
Daniel