Python Forum

Full Version: Json Parsing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Expert,

i wanted to extract below objects from json file, (valueObject,valueString)

"analyzeResult": {

    "documentResults": [

      {

        "docType": "custom:sampelform0205",

        "docTypeConfidence": 0.85,

        "fields": {

          "Report": {

            "type": "array",

            "valueArray": [

              {

                "type": "object",

                "valueObject": {

                  "machinary": {

                    "boundingBox": [

                      6.515,

                      1.62,

                      6.71,

                      1.62,

                      6.71,

                      1.675,

                      6.515,

                      1.675

                    ],

                    "elements": [

                      "#/readResults/0/lines/26/words/0"

                    ],

                    "page": 1,

                    "text": "34.70",

                    "type": "string",

                    "valueString": "34.70"

                  },
Could you help me with some sample code to extract
You should post a working data and what you have tried.
When coming from json and serialized to Python,the data is a dictionary.
This dictionary will usually be a mix of dict and list.
To fix a part of it and parse something.
data = {
    "valueObject": {
        "machinary": {
            "boundingBox": [
                6.515,
                1.62,
                6.71,
                1.62,
                6.71,
                1.675,
                6.515,
                1.675,
            ],
        },
    }
}
>>> data["valueObject"]
{'machinary': {'boundingBox': [6.515,
                               1.62,
                               6.71,
                               1.62,
                               6.71,
                               1.675,
                               6.515,
                               1.675]}}
>>> 
>>> data["valueObject"]["machinary"]["boundingBox"]
[6.515, 1.62, 6.71, 1.62, 6.71, 1.675, 6.515, 1.675]
>>> data["valueObject"]["machinary"]["boundingBox"][0]
6.515 
thanks will follow next time ... any sample code suggestion ... i am new to it
(May-04-2022, 12:56 PM)sshree43 Wrote: [ -> ]any sample code suggestion ... i am new to it
Not specific,search json parsing python on web or here on forum you will find a lot.
A tips is to learn about dictionary(this is what you access/parse from) dict the data structure get back when use,
json module or Requests very common way to get data(json) from eg a API.
Then test stuff out,post code(with error/Traceback) if have trouble.
suggestion please
(May-04-2022, 04:37 PM)sshree43 Wrote: [ -> ]suggestion please
You have gotten suggestion,i have even fixed part of you data and show how you parse it.
You have posted none working data and no code of what you have tried😒
Parsing of full data is just the same way as shown just start with key name in dictionary higher up.