skip to Main Content

I do have this sample json data that I got from jsonplaceholder, a dummy rest api data. I am practicing display/fetching data to it.

Now, I am having a trouble when I am fetching the name of the person. But look at the company, it also has the attribute name to it.

[
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "[email protected]",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    }
  },

so whenever I try to use $..name it displays the company name as well, which is I do not want I only want the name of the people. How do I separate it the name of the person and the name of the company.

I am using jsonpath as my playground to practice.

any ideas are greatly appreciated!

2

Answers


  1. You can just use this following script:

    const dataJson = require('./data.json');
    
    function main() {
        for (let i = 0; i < dataJson.length; i++) {
            console.log(dataJson[i].name)
        }
    }
    
    main()
    

    In your case, I got this output:

    $ node index.js 
    Leanne Graham
    John Carmack
    Mickael Jordan
    

    With this Json:

    [
      {
        "id": 1,
        "name": "Leanne Graham",
        "username": "Bret",
        "email": "[email protected]",
        "address": {
          "street": "Kulas Light",
          "suite": "Apt. 556",
          "city": "Gwenborough",
          "zipcode": "92998-3874",
          "geo": {
            "lat": "-37.3159",
            "lng": "81.1496"
          }
        },
        "phone": "1-770-736-8031 x56442",
        "website": "hildegard.org",
        "company": {
          "name": "Romaguera-Crona",
          "catchPhrase": "Multi-layered client-server neural-net",
          "bs": "harness real-time e-markets"
        }
      },
      {
        "id": 2,
        "name": "John Carmack",
        "username": "Bret",
        "email": "[email protected]",
        "address": {
          "street": "Kulas Light",
          "suite": "Apt. 556",
          "city": "Gwenborough",
          "zipcode": "92998-3874",
          "geo": {
            "lat": "-37.3159",
            "lng": "81.1496"
          }
        },
        "phone": "1-770-736-8031 x56442",
        "website": "hildegard.org",
        "company": {
          "name": "Romaguera-Crona",
          "catchPhrase": "Multi-layered client-server neural-net",
          "bs": "harness real-time e-markets"
        }
      },
      {
        "id": 3,
        "name": "Mickael Jordan",
        "username": "Bret",
        "email": "[email protected]",
        "address": {
          "street": "Kulas Light",
          "suite": "Apt. 556",
          "city": "Gwenborough",
          "zipcode": "92998-3874",
          "geo": {
            "lat": "-37.3159",
            "lng": "81.1496"
          }
        },
        "phone": "1-770-736-8031 x56442",
        "website": "hildegard.org",
        "company": {
          "name": "Romaguera-Crona",
          "catchPhrase": "Multi-layered client-server neural-net",
          "bs": "harness real-time e-markets"
        }
      }
    ]
    
    Login or Signup to reply.
  2. The jsonpath for this query would be $[*].name which means "for any element in the root array get the name field". This avoids using .. which is interpreted as recursive descent (i.e. looking at any and all child objects).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search