Debug School

rakesh kumar
rakesh kumar

Posted on

How to find length of key value pair in json

Find length of keys from json json object
Find length of value from json json object
Find no of key value pair from json object

Find key word from json object key value pair
Find value word from json object key value pair
Find no of key value pair from json object

my json data

{
    "InProgress": "2024-02-15T04:54:43.677934Z",
    "Approve": "2024-02-15T04:54:50.944840Z"
}
Enter fullscreen mode Exit fullscreen mode
var numberOfPairs = Object.entries(pub_status).length;
Enter fullscreen mode Exit fullscreen mode
var pub_status = record.publisher_status;    
        var pub_statusdata = record.publisher_date ? JSON.parse(record.publisher_date) : {};

  var pub_keyvalue_length = pub_statusdata ? Object.entries(pub_statusdata).length : null;
Enter fullscreen mode Exit fullscreen mode

Find length of keys from json json object

var keysLength = pub_status ? Object.keys(pub_status).length : null;
Enter fullscreen mode Exit fullscreen mode
var pub_status = record.publisher_status;    
        var pub_statusdata = record.publisher_date ? JSON.parse(record.publisher_date) : {};

    var pub_status_keylength = pub_statusdata ? Object.keys(pub_statusdata)[0].length : null;
Enter fullscreen mode Exit fullscreen mode

Find length of value from json json object

var numberOfValues = Object.values(pub_status).length;
Enter fullscreen mode Exit fullscreen mode
var pub_status = record.publisher_status;    
        var pub_statusdata = record.publisher_date ? JSON.parse(record.publisher_date) : {};

   var pub_status_valuelength = pub_statusdata ? Object.values(pub_statusdata)[0].length : null;
Enter fullscreen mode Exit fullscreen mode

Find key word from json object key value pair

var pub_status_key = pub_statusdata ? Object.keys(pub_statusdata)[0] : null;
Enter fullscreen mode Exit fullscreen mode

Find value word from json object key value pair

 var pub_status_value = pub_statusdata ? Object.values(pub_statusdata)[0] : null;
Enter fullscreen mode Exit fullscreen mode

Full code

 {
    "InProgress": "2024-02-15T04:54:43.677934Z",
    "Approve": "2024-02-15T04:54:50.944840Z"
}
Enter fullscreen mode Exit fullscreen mode
     var pub_status = record.publisher_status;    
        var pub_statusdata = record.publisher_date ? JSON.parse(record.publisher_date) : {};

        var pub_status_key = pub_statusdata ? Object.keys(pub_statusdata)[0] : null;
        var pub_status_keylength = pub_statusdata ? Object.keys(pub_statusdata)[0].length : null;
        var pub_status_value = pub_statusdata ? Object.values(pub_statusdata)[0] : null;
        var pub_status_valuelength = pub_statusdata ? Object.values(pub_statusdata)[0].length : null;
        var pub_keyvalue_length = pub_statusdata ? Object.entries(pub_statusdata).length : null;
       // var keysLength = pub_status ? Object.keys(pub_status).length : null;
        console.log("pub_statusdata");
          console.log(pub_statusdata);
          console.log(pub_status_key);
          console.log(pub_status_keylength);
          console.log(pub_status_value);
          console.log(pub_status_valuelength);
          console.log(pub_keyvalue_length);
Enter fullscreen mode Exit fullscreen mode

output

Image description

Top comments (0)