How to count number of a particular item in a JSON response?

Hi,

I have this JSON response -

and would like to know how I would go about counting the number of line_items (it will vary between responses).

Thanks
Steve

From your screenshot, it looks like the “line_items” property may not be an array/list. So it could be tricky to figure out that count.

Can you post the full JSON response as text (formatted using the </> in the forums toolbar)?

It’s really hard to troubleshoot JSON without the full response as text.

Seemed to have deleted the original one. This one only has 4 line items (document.inference.line_items) but same concept. Thanks.

Can’t find the </> so hope this is what you mean -

{
    "api_request": {
        "error": {},
        "resources": [
            "document"
        ],
        "status": "success",
        "status_code": 201,
        "url": "https://api.mindee.net/v1/products/mindee/expense_receipts/v5/predict"
    },
    "document": {
        "id": "0e785303-d233-4158-85f4-1f05ed3df77a",
        "inference": {
            "extras": {},
            "finished_at": "2023-09-15T11:35:19.630290",
            "is_rotation_applied": true,
            "pages": [
                {
                    "extras": {},
                    "id": 0,
                    "orientation": {
                        "value": 0
                    },
                    "prediction": {
                        "category": {
                            "confidence": 0.73,
                            "value": "food"
                        },
                        "date": {
                            "confidence": 0.99,
                            "polygon": [
                                [
                                    0.333,
                                    0.91
                                ],
                                [
                                    0.506,
                                    0.91
                                ],
                                [
                                    0.506,
                                    0.931
                                ],
                                [
                                    0.333,
                                    0.931
                                ]
                            ],
                            "value": "2020-10-17"
                        },
                        "document_type": {
                            "confidence": 1.0,
                            "value": "EXPENSE RECEIPT"
                        },
                        "line_items": [
                            {
                                "confidence": 0.96,
                                "description": "HAND TOWEL",
                                "polygon": [
                                    [
                                        0.05,
                                        0.239
                                    ],
                                    [
                                        0.91,
                                        0.239
                                    ],
                                    [
                                        0.91,
                                        0.259
                                    ],
                                    [
                                        0.05,
                                        0.259
                                    ]
                                ],
                                "quantity": null,
                                "total_amount": 2.97,
                                "unit_price": null
                            },
                            {
                                "confidence": 0.95,
                                "description": "GATORADE",
                                "polygon": [
                                    [
                                        0.05,
                                        0.266
                                    ],
                                    [
                                        0.908,
                                        0.266
                                    ],
                                    [
                                        0.908,
                                        0.286
                                    ],
                                    [
                                        0.05,
                                        0.286
                                    ]
                                ],
                                "quantity": null,
                                "total_amount": 2.0,
                                "unit_price": null
                            },
                            {
                                "confidence": 0.95,
                                "description": "T-SHIRT",
                                "polygon": [
                                    [
                                        0.05,
                                        0.29
                                    ],
                                    [
                                        0.908,
                                        0.29
                                    ],
                                    [
                                        0.908,
                                        0.312
                                    ],
                                    [
                                        0.05,
                                        0.312
                                    ]
                                ],
                                "quantity": null,
                                "total_amount": 16.88,
                                "unit_price": null
                            },
                            {
                                "confidence": 0.97,
                                "description": "PUSH PINS",
                                "polygon": [
                                    [
                                        0.05,
                                        0.317
                                    ],
                                    [
                                        0.908,
                                        0.317
                                    ],
                                    [
                                        0.908,
                                        0.338
                                    ],
                                    [
                                        0.05,
                                        0.338
                                    ]
                                ],
                                "quantity": null,
                                "total_amount": 1.24,
                                "unit_price": null
                            }
                        ],
                        "locale": {
                            "confidence": 0.8,
                            "country": "US",
                            "currency": "USD",
                            "language": "en",
                            "value": "en-US"
                        },
                        "orientation": {
                            "confidence": 0.99,
                            "degrees": 0
                        },
                        "subcategory": {
                            "confidence": 0.94,
                            "value": "shopping"
                        },
                        "supplier_address": {
                            "confidence": 0.92,
                            "polygon": [
                                [
                                    0.303,
                                    0.127
                                ],
                                [
                                    0.69,
                                    0.127
                                ],
                                [
                                    0.69,
                                    0.198
                                ],
                                [
                                    0.303,
                                    0.198
                                ]
                            ],
                            "value": "manager tod linga 888 wall store st wall st city la 88888"
                        },
                        "supplier_company_registrations": [],
                        "supplier_name": {
                            "confidence": 0.0,
                            "polygon": [],
                            "raw_value": null,
                            "value": null
                        },
                        "supplier_phone_number": {
                            "confidence": 0.63,
                            "polygon": [
                                [
                                    0.356,
                                    0.101
                                ],
                                [
                                    0.64,
                                    0.101
                                ],
                                [
                                    0.64,
                                    0.128
                                ],
                                [
                                    0.356,
                                    0.128
                                ]
                            ],
                            "value": "(888)8888888"
                        },
                        "taxes": [
                            {
                                "base": null,
                                "code": "TAX",
                                "confidence": 0.99,
                                "polygon": [
                                    [
                                        0.256,
                                        0.401
                                    ],
                                    [
                                        0.915,
                                        0.401
                                    ],
                                    [
                                        0.915,
                                        0.423
                                    ],
                                    [
                                        0.256,
                                        0.423
                                    ]
                                ],
                                "rate": 4.9,
                                "value": 1.28
                            },
                            {
                                "base": null,
                                "code": "TAX",
                                "confidence": 0.99,
                                "polygon": [
                                    [
                                        0.255,
                                        0.378
                                    ],
                                    [
                                        0.915,
                                        0.378
                                    ],
                                    [
                                        0.915,
                                        0.4
                                    ],
                                    [
                                        0.255,
                                        0.4
                                    ]
                                ],
                                "rate": 7.9,
                                "value": 2.9
                            }
                        ],
                        "time": {
                            "confidence": 0.99,
                            "polygon": [
                                [
                                    0.579,
                                    0.91
                                ],
                                [
                                    0.667,
                                    0.91
                                ],
                                [
                                    0.667,
                                    0.931
                                ],
                                [
                                    0.579,
                                    0.931
                                ]
                            ],
                            "value": "16:12"
                        },
                        "tip": {
                            "confidence": 0.0,
                            "polygon": [],
                            "value": null
                        },
                        "total_amount": {
                            "confidence": 0.99,
                            "polygon": [
                                [
                                    0.825,
                                    0.426
                                ],
                                [
                                    0.915,
                                    0.426
                                ],
                                [
                                    0.915,
                                    0.444
                                ],
                                [
                                    0.825,
                                    0.444
                                ]
                            ],
                            "value": 27.27
                        },
                        "total_net": {
                            "confidence": 0.99,
                            "polygon": [
                                [
                                    0.825,
                                    0.355
                                ],
                                [
                                    0.913,
                                    0.355
                                ],
                                [
                                    0.913,
                                    0.375
                                ],
                                [
                                    0.825,
                                    0.375
                                ]
                            ],
                            "value": 23.09
                        },
                        "total_tax": {
                            "confidence": 0.99,
                            "polygon": [],
                            "value": 4.18
                        }
                    }
                }
            ],
            "prediction": {
                "category": {
                    "confidence": 0.73,
                    "value": "food"
                },
                "date": {
                    "confidence": 0.99,
                    "page_id": 0,
                    "polygon": [
                        [
                            0.333,
                            0.91
                        ],
                        [
                            0.506,
                            0.91
                        ],
                        [
                            0.506,
                            0.931
                        ],
                        [
                            0.333,
                            0.931
                        ]
                    ],
                    "value": "2020-10-17"
                },
                "document_type": {
                    "confidence": 1.0,
                    "value": "EXPENSE RECEIPT"
                },
                "line_items": [
                    {
                        "confidence": 0.96,
                        "description": "HAND TOWEL",
                        "page_id": 0,
                        "polygon": [
                            [
                                0.05,
                                0.239
                            ],
                            [
                                0.91,
                                0.239
                            ],
                            [
                                0.91,
                                0.259
                            ],
                            [
                                0.05,
                                0.259
                            ]
                        ],
                        "quantity": null,
                        "total_amount": 2.97,
                        "unit_price": null
                    },
                    {
                        "confidence": 0.95,
                        "description": "GATORADE",
                        "page_id": 0,
                        "polygon": [
                            [
                                0.05,
                                0.266
                            ],
                            [
                                0.908,
                                0.266
                            ],
                            [
                                0.908,
                                0.286
                            ],
                            [
                                0.05,
                                0.286
                            ]
                        ],
                        "quantity": null,
                        "total_amount": 2.0,
                        "unit_price": null
                    },
                    {
                        "confidence": 0.95,
                        "description": "T-SHIRT",
                        "page_id": 0,
                        "polygon": [
                            [
                                0.05,
                                0.29
                            ],
                            [
                                0.908,
                                0.29
                            ],
                            [
                                0.908,
                                0.312
                            ],
                            [
                                0.05,
                                0.312
                            ]
                        ],
                        "quantity": null,
                        "total_amount": 16.88,
                        "unit_price": null
                    },
                    {
                        "confidence": 0.97,
                        "description": "PUSH PINS",
                        "page_id": 0,
                        "polygon": [
                            [
                                0.05,
                                0.317
                            ],
                            [
                                0.908,
                                0.317
                            ],
                            [
                                0.908,
                                0.338
                            ],
                            [
                                0.05,
                                0.338
                            ]
                        ],
                        "quantity": null,
                        "total_amount": 1.24,
                        "unit_price": null
                    }
                ],
                "locale": {
                    "confidence": 0.8,
                    "country": "US",
                    "currency": "USD",
                    "language": "en",
                    "value": "en-US"
                },
                "subcategory": {
                    "confidence": 0.94,
                    "value": "shopping"
                },
                "supplier_address": {
                    "confidence": 0.92,
                    "page_id": 0,
                    "polygon": [
                        [
                            0.303,
                            0.127
                        ],
                        [
                            0.69,
                            0.127
                        ],
                        [
                            0.69,
                            0.198
                        ],
                        [
                            0.303,
                            0.198
                        ]
                    ],
                    "value": "manager tod linga 888 wall store st wall st city la 88888"
                },
                "supplier_company_registrations": [],
                "supplier_name": {
                    "confidence": 0.0,
                    "page_id": null,
                    "polygon": [],
                    "raw_value": null,
                    "value": null
                },
                "supplier_phone_number": {
                    "confidence": 0.63,
                    "page_id": 0,
                    "polygon": [
                        [
                            0.356,
                            0.101
                        ],
                        [
                            0.64,
                            0.101
                        ],
                        [
                            0.64,
                            0.128
                        ],
                        [
                            0.356,
                            0.128
                        ]
                    ],
                    "value": "(888)8888888"
                },
                "taxes": [
                    {
                        "base": null,
                        "code": "TAX",
                        "confidence": 0.99,
                        "page_id": 0,
                        "polygon": [
                            [
                                0.256,
                                0.401
                            ],
                            [
                                0.915,
                                0.401
                            ],
                            [
                                0.915,
                                0.423
                            ],
                            [
                                0.256,
                                0.423
                            ]
                        ],
                        "rate": 4.9,
                        "value": 1.28
                    },
                    {
                        "base": null,
                        "code": "TAX",
                        "confidence": 0.99,
                        "page_id": 0,
                        "polygon": [
                            [
                                0.255,
                                0.378
                            ],
                            [
                                0.915,
                                0.378
                            ],
                            [
                                0.915,
                                0.4
                            ],
                            [
                                0.255,
                                0.4
                            ]
                        ],
                        "rate": 7.9,
                        "value": 2.9
                    }
                ],
                "time": {
                    "confidence": 0.99,
                    "page_id": 0,
                    "polygon": [
                        [
                            0.579,
                            0.91
                        ],
                        [
                            0.667,
                            0.91
                        ],
                        [
                            0.667,
                            0.931
                        ],
                        [
                            0.579,
                            0.931
                        ]
                    ],
                    "value": "16:12"
                },
                "tip": {
                    "confidence": 0.0,
                    "page_id": null,
                    "polygon": [],
                    "value": null
                },
                "total_amount": {
                    "confidence": 0.99,
                    "page_id": 0,
                    "polygon": [
                        [
                            0.825,
                            0.426
                        ],
                        [
                            0.915,
                            0.426
                        ],
                        [
                            0.915,
                            0.444
                        ],
                        [
                            0.825,
                            0.444
                        ]
                    ],
                    "value": 27.27
                },
                "total_net": {
                    "confidence": 0.99,
                    "page_id": 0,
                    "polygon": [
                        [
                            0.825,
                            0.355
                        ],
                        [
                            0.913,
                            0.355
                        ],
                        [
                            0.913,
                            0.375
                        ],
                        [
                            0.825,
                            0.375
                        ]
                    ],
                    "value": 23.09
                },
                "total_tax": {
                    "confidence": 0.99,
                    "page_id": 0,
                    "polygon": [],
                    "value": 4.18
                }
            },
            "processing_time": 2.439,
            "product": {
                "features": [
                    "locale",
                    "category",
                    "subcategory",
                    "document_type",
                    "date",
                    "time",
                    "total_amount",
                    "total_net",
                    "total_tax",
                    "tip",
                    "taxes",
                    "supplier_name",
                    "supplier_company_registrations",
                    "supplier_address",
                    "supplier_phone_number",
                    "orientation",
                    "line_items"
                ],
                "name": "mindee/expense_receipts",
                "type": "standard",
                "version": "5.0"
            },
            "started_at": "2023-09-15T11:35:17.191564"
        },
        "n_pages": 1,
        "name": "receipt.png"
    }
}

No, that was quoted instead of preformatted. I’ve fixed it for you and the JSON is now valid. The button is here:

Screenshot 2023-09-15 at 8.02.51 AM

When pasting your JSON response into Best JSON Viewer and JSON Beautifier Online, I can see that the “line_items” property is indeed an array/list and the path to that property is at the top of this screenshot:

So we can get the length of the list at the property “document.inference.pages[1].prediction.line_items” of the JSON object.

Thanks.

As an aside the preformatted icon does not show up on my toolbar but I found it under the gear icon.

Oh interesting, thanks for letting me know that. I assumed it was in everyone’s main toolbar.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.