Froddo Baggins
Froddo Baggins

Reputation: 53

How to parse only certain properties and values from JSON

I am trying to parse a JSON. Example below,

[
{
    "id": "(error)",
    "raw": "Expected an assignment or function call and instead saw an expression.",
    "code": "W030",
    "evidence": "v",
    "line": 1,
    "character": 1,
    "scope": "(main)",
    "reason": "Expected an assignment or function call and instead saw an expression."
  },
  {
    "id": "(error)",
    "raw": "Missing semicolon.",
    "code": "W033",
    "evidence": "v",
    "line": 1,
    "character": 2,
    "scope": "(main)",
    "reason": "Missing semicolon."
  }
] 

I just need to print something like the following:

I have a DEMO. How can I just parse only the "reason" properties/values and how many times it is occurring?

Upvotes: 4

Views: 532

Answers (4)

TGH
TGH

Reputation: 39258

var obj = 
    [
    {
        "id": "(error)",
        "raw": "Expected an assignment or function call and instead saw an expression.",
        "code": "W030",
        "evidence": "v",
        "line": 1,
        "character": 1,
        "scope": "(main)",
        "reason": "Expected an assignment or function call and instead saw an expression."
      },
      {
        "id": "(error)",
        "raw": "Missing semicolon.",
        "code": "W033",
        "evidence": "v",
        "line": 1,
        "character": 2,
        "scope": "(main)",
        "reason": "Missing semicolon."
      }
    ] ;

alert(obj[0].reason);
alert(obj[1].reason);

Upvotes: 1

sawan
sawan

Reputation: 2381

you can do it by this way...

step 1: convert json to array.

var tags = $.parseJSON(json_string);

step 2: remove all 'raw' to error string array and print them

var errors= new Array();
var error_no = 1;
for (var i = 0; i < tags.length; i++) {
     var element = tags [i];
     var e_String = 'Reason '+error_no;
     error_no++;
     error.e_String = element.raw;
}

Upvotes: 0

Endre Simo
Endre Simo

Reputation: 11551

Just loop through the array object and search for the values you need:

var obj = [
    {
        "id": "(error)",
        "raw": "Expected an assignment or function call and instead saw an expression.",
        "code": "W030",
        "evidence": "v",
        "line": 1,
        "character": 1,
        "scope": "(main)",
        "reason": "Expected an assignment or function call and instead saw an expression."
      },
      {
        "id": "(error)",
        "raw": "Missing semicolon.",
        "code": "W033",
        "evidence": "v",
        "line": 1,
        "character": 2,
        "scope": "(main)",
        "reason": "Missing semicolon."
      }
    ] ;

$.each(obj, function(key, value){    
    if (value && value.hasOwnProperty('reason')){
        console.log(value['reason']);
    }
}); 

Upvotes: 1

Dart
Dart

Reputation: 787

if you want to print errors try this:

Javascript

$(document).ready(function(){
    $("#js").keyup(function(){
        jsSource = $("#js").val();
        JSHINT(jsSource);
        result = JSON.stringify(JSHINT.errors, null, 2);
        //storage for reasons
        var errors = [];
        for(var i = 0; i < JSHINT.errors.length; i++){
            //reason collecting
            errors.push((i + 1) + ') ' + JSHINT.errors[i].reason);
        }
        //print reasons
        document.getElementById('output').innerHTML = errors.join(', ');
    });
});

Upvotes: 1

Related Questions