Counting Items in JSON result

python count unique values in json
python json count occurrences
count number of records in json python
count number of records in json file
how to access json data in python
count items in json
query json python
json to table python

I am counting the amount of rows is my JSON result however I am also trying to count the number of items in the result.

The row count works but the item count won't. Getting error : Trying to get property of non-object on the $FLCount line

Code is below

// Counts The Rows - WORKS 
$row = $obj->response->result->Accounts->row;
$countRows = count($row);

// Counts The FL Items ( Doesn't Work )
$FLCount= $obj->response->result->Accounts->row->FL;
$countItems = count($FLCount);

JSON Result Snippet

{
  "response": {
    "result": {
      "Accounts": {
        "row": [
          {
            "no": "1",
            "FL": [
              {
                "val": "ITEM 1",
                "content": "XX"
              },
             {
                "val": "ITEM 2",
                "content": "XX"
              },
              {
                "val": "ITEM 3",
                "content": "XX"
              }
            ]
          }
        ]
      }
    }
  }
}
$FLCount= $obj->response->result->Accounts->row[0]->FL;

$countItems = count($FLCount);

How to count items in JSON data, The goal is to loop through each object in a list of JSON objects and count produce a single object as the result containing the counts of allĀ  This article takes a look at an exercise that explains how many ways there are to count the values in a JSON array returned from a REST API call.

You have the error because $obj->response->result->Accounts->row is not an object but an array of rows.

To count all items no matter the row you can just loop through the rows and add up the number of items in each :

// Counts The Rows - WORKS 
$rows = $obj->response->result->Accounts->row;
$countRows = count($rows);

// Counts The FL Items ( Doesn't Work )
$countItems = 0;
foreach ($rows as $row) {
    $countItems += count($row->FL);
}

JSON parsing: counting, Solved: I am calling an API which returns a JSON object as the body of the response as a flow file. I want to count the total records in the flow. This is the sample JSON the api response body returns. The records are the items of the list which is the value of the "result" key In the above sample there are 2 records (2 items in the list).

(EDIT: Unfortunately, I've misunderstood your requirement, and my answer addresses the solution to count it in JAVASCRIPT, instead of php)

1st problem:

One is array, another is object. In such cases, you can use:

element.length

instead of

count(element)
2nd problem:

Your actual problem is nicely expressed by user @niiwig ! However, to make it shorter, use:

var amount=0; JSON.parse(YOUR_RESPONSE).response.result.Accounts.row.every((obj) => amount += obj.FL.length  ); 

and amount variable will have the correct number.

How to count the number of records of a JSON object flow file in NiFi?, request data; count items. I used Thingifier, created a bunch of 'todos' andĀ  JSON objects are surrounded by curly braces {}. JSON objects are written in key/value pairs. Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null). Keys and values are separated by a colon. Each key/value pair is separated by a comma.

Exercise, console.log(obj.count);. 5 See the JSON object for a description of JSON syntax. reviver The Object corresponding to the given JSON text . Each item in the array is: the question's ID, it's place on the form, and the response value or in the case of a multi-select or drop down the 'values'. I only care about the ID, and it's value(s). I'm wanting to get these value fields into the appropriate CRM field.

JSON.parse(), find a REST API that returns a JSON array object e.g.; how can you identify how many "to-do's" are in the list. To take part, you'll need to:. JSON is a format for storing and transporting data. JSON is often used when data is sent from a server to a web page. * The JSON syntax is derived from JavaScript object notation syntax, but the JSON format is text only. Code for reading and generating JSON data can be written in any programming language. This JSON syntax defines an employees

Count Values in a JSON Array Returned From a REST API Call , Now, when you use a for loop to iterate and count a number of properties, it will also count the enumerable properties of the animal object. Solved: Hi, There is a function to count the number of items which are in a collection? Thanks.

Comments
  • FL is not a direct child of row so you need to access it by either looping row and accessing FL or hard-coding a row position $obj->response->result->Accounts->row[0]->FL;
  • See the Accessing nested items section of How do I extract data from JSON with PHP?
  • ahh keep forgetting the [0] bit
  • dont need to loop through all rows as the item count is the same in each row
  • oh. Then you could just do $couldItems = $countRows * count($obj->response->result->Accounts->row[0]->FL);