r/programminghelp • • Mar 30 '22

Python help with JSON parsing in python

Hey everyone.

I'd just like to preface this by saying that I hate microsoft.

So I'm trying to automate some shit with power BI, and i found thaht you can edit the Layouts file in the .pbix archive. in so doing, I've found that it's a json formatted file. the problem is that on certain fields, the value is more json, but formatted as a string for some reason. It looks like this, for example.

"config":"{\"name\":\"7d1d23a700a5e62cd3ae\",\"layouts\":[{\"id\":0,\"position\":{\"x\":10,\"y\":0,\"width\":500,\"height\":500,\"z\":0}}],\"singleVisual\":{\"visualType\":\"textbox\",\"drillFilterOtherVisuals\":true,\"objects\":{\"general\":[{\"properties\":{\"paragraphs\":[{\"textRuns\":[{\"value\":\".pyiioeuioeui ofh od\",\"textStyle\":{\"color\":\"#ffffff\"}}]}]}}]},\"vcObjects\":{\"background\":[{\"properties\":{\"show\":{\"expr\":{\"Literal\":{\"Value\":\"true\"}}}}}]}}}","filters":"[]"}

or something like that.

so what I've done is I've made a program that makes it look pretty so you can see the parameters more easily. Now I want to be able to load a normal json fromatted file up to the Layouts file. Meaning that I'll need to implement some fucky wucky shit to make it look like garbage so PowerBI will be able to read it.

Where I'm stuck is, I don't really know much about parsing json enough to be able to go through and find all the 'config' parameters to be able to do this to them. It's all nested json, so when i do a json.loads() on the data, it only shows the first level of json, but doesn't go deeper... I was about to do a regex on it to find all the instances of '"config" : { ' but that has the drawback that it's regex and there's not really a good way of finding the end of that string.

Is there a relatively easy way of doing this with the json module? Or is my best bet to just hack it?

5 Upvotes

7 comments sorted by

1

u/Goobyalus Mar 30 '22

Could you elaborate on:

It's all nested json, so when i do a json.loads() on the data, it only shows the first level of json, but doesn't go deeper...

or maybe print a loaded JSON and post that?

Not sure if the problem is that it's malformed and loading weirdly, or if all that's needed is a recursive search.

1

u/MFAFuckedMe Mar 30 '22

{ "id": 0, "theme": "Custom284576015400438.json", "resourcePackages": [ { "resourcePackage": { "name": "SharedResources", "type": 2, "items": [ { "type": 202, "path": "BaseThemes/CY18SU07.json", "name": "CY18SU07" } ], "disabled": false } }, { "resourcePackage": { "name": "RegisteredResources", "type": 1, "items": [ { "type": 201, "path": "Custom284576015400438.json", "name": "Custom284576015400438.json" } ], "disabled": false } } ], "sections": [ { "id": 0, "name": "ReportSection", "displayName": "Page 1", "filters": "[]", "ordinal": 0, "visualContainers": [ { "x": 10, "y": 0, "z": 0, "width": 500, "height": 500, "config": { "name": "7d1d23a700a5e62cd3ae", "layouts": [ { "id": 0, "position": { "x": 10, "y": 0, "width": 500, "height": 500, "z": 0 } } ], "singleVisual": { "visualType": "textbox", "drillFilterOtherVisuals": true, "objects": { "general": [ { "properties": { "paragraphs": [ { "textRuns": [ { "value": ".pyiioeuioeui ofh od", "textStyle": { "color": "#ffffff" } } ] } ] } } ] }, "vcObjects": { "background": [ { "properties": { "show": { "expr": { "Literal": { "Value": "true" } } } } } ] } } }, "filters": "[]" },

1

u/MFAFuckedMe Mar 30 '22

for some reason reddit isn't formatting it as inline code,

but basically, if you look at something like resource packages, it's another nested layer. And it get's more confusing when you look at config, because that's formatted as a long string instead of actual JSON, and each of the " is escaped with a \ in the config string like this

"config":"{\"name\":\"7d1d23a700a5e62cd3ae\",\"layouts\":[{\"id\":0,\"position\":{\"x\":10,\"y\":0,\"width\":500,\"height\":500,\"z\":0}}],\"singleVisual\":{\"visualType\":\"textbox\",\"drillFilterOtherVisuals\":true,\"objects\":{\"general\":[{\"properties\":{\"paragraphs\":[{\"textRuns\":[{\"value\":\".pyiioeuioeui ofh od\",\"textStyle\":{\"color\":\"#ffffff\"}}]}]}}]}

1

u/Goobyalus Apr 01 '22

Sorry, I missed your comments.

Where you show a JSON containing the string of another JSON, is that what you're talking about by "nested" JSON?? Awfull stuff.

But I don't see that in the formatted example you have in the other comment.

idk how many levels of nested JSON you have, or ow many are even possible, but if you know some value is a string of a JSON, you would just do another json.loads on that string. E.g. if I use your example (and terminate it with the necessary missing braces and quote):

>>> d = {"config":"{\"name\":\"7d1d23a700a5e62cd3ae\",\"layouts\":[{\"id\":0,\"position\":{\"x\":10,\"y\":0,\"width\":500,\"height\":500,\"z\":0}}],\"singleVisual\":{\"visualType\":\"textbox\",\"drillFilterOtherVisuals\":true,\"objects\":{\"general\":[{\"properties\":{\"paragraphs\":[{\"textRuns\":[{\"value\":\".pyiioeuioeui ofh od\",\"textStyle\":{\"color\":\"#ffffff\"}}]}]}}]}}}"}

>>> js = d['config']

>>> js
'{"name":"7d1d23a700a5e62cd3ae","layouts":[{"id":0,"position":{"x":10,"y":0,"width":500,"height":500,"z":0}}],"singleVisual":{"visualType":"textbox","drillFilterOtherVisuals":true,"objects":{"general":[{"properties":{"paragraphs":[{"textRuns":[{"value":".pyiioeuioeui ofh od","textStyle":{"color":"#ffffff"}}]}]}}]}}}'

>>> json.loads(js)
{'name': '7d1d23a700a5e62cd3ae', 'layouts': [{'id': 0, 'position': {'x': 10, 'y': 0, 'width': 500, 'height': 500, 'z': 0}}], 'singleVisual': {'visualType': 'textbox', 'drillFilterOtherVisuals': True, 'objects': {'general': [{'properties': {'paragraphs': [{'textRuns': [{'value': '.pyiioeuioeui ofh od', 'textStyle': {'color': '#ffffff'}}]}]}}]}}}

>>>

2

u/serg06 Mar 30 '22

Here you go:

{
  "id": 0,
  "resourcePackages":
  [
    {
      "resourcePackage":
      {
        "disabled": false,
        "items":
        [
          {
            "name": "CY18SU07",
            "path": "BaseThemes/CY18SU07.json",
            "type": 202
          }
        ],
        "name": "SharedResources",
        "type": 2
      }
    },
    {
      "resourcePackage":
      {
        "disabled": false,
        "items":
        [
          {
            "name": "Custom284576015400438.json",
            "path": "Custom284576015400438.json",
            "type": 201
          }
        ],
        "name": "RegisteredResources",
        "type": 1
      }
    }
  ],
  "sections":
  [
    {
      "displayName": "Page 1",
      "filters": "[]",
      "id": 0,
      "name": "ReportSection",
      "ordinal": 0,
      "visualContainers":
      [
        {
          "config":
          {
            "layouts":
            [
              {
                "id": 0,
                "position":
                {
                  "height": 500,
                  "width": 500,
                  "x": 10,
                  "y": 0,
                  "z": 0
                }
              }
            ],
            "name": "7d1d23a700a5e62cd3ae",
            "singleVisual":
            {
              "drillFilterOtherVisuals": true,
              "objects":
              {
                "general":
                [
                  {
                    "properties":
                    {
                      "paragraphs":
                      [
                        {
                          "textRuns":
                          [
                            {
                              "textStyle":
                              {
                                "color": "#ffffff"
                              },
                              "value": ".pyiioeuioeui ofh od"
                            }
                          ]
                        }
                      ]
                    }
                  }
                ]
              },
              "vcObjects":
              {
                "background":
                [
                  {
                    "properties":
                    {
                      "show":
                      {
                        "expr":
                        {
                          "Literal":
                          {
                            "Value": "true"
                          }
                        }
                      }
                    }
                  }
                ]
              },
              "visualType": "textbox"
            }
          },
          "filters": "[]",
          "height": 500,
          "width": 500,
          "x": 10,
          "y": 0,
          "z": 0
        }
      ]
    }
  ],
  "theme": "Custom284576015400438.json"
}

1

u/serg06 Mar 30 '22

Where I'm stuck is, I don't really know much about parsing json enough to be able to go through and find all the 'config' parameters to be able to do this to them.

Are you asking how to parse the json and get the config section?

You can get the first one like so: https://onlinegdb.com/q96AqlemV

2

u/MFAFuckedMe Mar 30 '22

Exactly, thank you