EN VI

Separating the common name when extracting data from a json format?

2024-03-11 18:00:07
Separating the common name when extracting data from a json format

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!

Solution:

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).

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login