-3

I request a code modification that extracts columns from a (nested) JSON file inside a '.PBIX' file (Power BI tool) in Python. The details are below:

Request:

  • I need to extract the 'visualType' field as well in a similar format in the GitHub code above for Business Documentation. To be able to extract parameters like this in a table automatically will save hours of time in documentation.

I tried:

  • JS Beautify JSON dumps, JSON LOADS, JSON Normalize, Different Encoding types, Adding ['visualTypes'] in code but didn't know how to append. I have trouble understanding the structure of the JSON file here, too.

Other tries :

!cd "path/Layout"
import json

# Opening JSON file
f = open('path/Layout', encoding='utf-16 le')

# returns JSON object as
# a dictionary
data = json.load(f)

# Iterating through the json
# list
for i in data:
    print(i)

###Output: ###

  • id sections pods config reportId resourcePackages layoutOptimization publicCustomVisuals
Parin
  • 1
  • 1
  • You need to wash this structure it is a mix of json and json strings, You have 2 "Layout" fields embeded into json strings. it is a good excersise – MortenB Sep 29 '22 at 06:32
  • Thanks for your quick response. I'll also continue to learn the things required to understand and solve the problem. Have a good day. – Parin Sep 29 '22 at 06:41

1 Answers1

0

Main reason this was downwoted what that you did not post any code or errors when you tried to solve this.

When I tried I got this error: json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

and: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa9 in position 9380: invalid start byte

This you can most likely find lots of answers to with google:

When you see byte errors in json.load() it is most likely encoding, so chardet will help you:

#!/usr/bin/env python3
import json
import chardet
raw = "C:\\Users\\mobj\\Downloads\\Layout"

with open(raw, 'rb') as FI:
    print(chardet.detect(FI.read()))

dat = {}
# encoding from chardet:
with open(raw, 'r', encoding='utf-16le', errors='ignore') as FI:
    dat = json.load(FI)

print(json.dumps(dat, indent=2, sort_keys=True))
MortenB
  • 2,749
  • 1
  • 31
  • 35
  • 1
    Thanks so much for this response and your time. I deeply appreciate it. In my code, the error was caused by not exactly inputting 'encoding='utf-16 le' while reading the JSON file of PBIX. The original author Mr Grando successfully avoided this by always keeping that line while reading JSON file in python, as I understand it. – Parin Sep 29 '22 at 07:33