Jyoti
Jyoti

Reputation: 47

How to get the values from JSON where JSON starts with Array Block in RestAssured

I am automating twitter API using RestAssured

I am trying to fetch the response from twitter API:

String responseTweets = given().header("Authorization", "Bearer" +" "+token)
                .queryParam("count", "5")
                .queryParam("screen_name", "twitterapi")
                .when().log().all()
                .get("https://api.twitter.com/1.1/statuses/user_timeline.json").asString();

Response:

[
  {
    "created_at": "Wed Jun 30 17:30:46 +0000 2021",
    "id": 1410289721977176000,
    "id_str": "1410289721977176066",
    "text": "Today, we’re launching new manage mutes into Twitter API v2.\n\n#TwitterAPI #v2 #EarlyAccess",
    "truncated": false,
    "entities": {
      "hashtags": [
        {
          "text": "TwitterAPI",
          "indices": [
            62,
            73
          ]
        },
        {
          "text": "v2",
          "indices": [
            74,
            77
          ]
        },
        {
          "text": "EarlyAccess",
          "indices": [
            78,
            90
          ]
        }
      ],
      "symbols": [],
      "user_mentions": [],
      "urls": [
        {
          "url": "",
          "expanded_url": "https://twitter.com/TwitterDev/status/1410289221894651907",
          "display_url": "twitter.com/TwitterDev/sta…",
          "indices": [
            91,
            114
          ]
        }
      ]
    },
{
    "created_at": "Tue Jun 29 20:44:15 +0000 2021",
    "id": 1409976027426611200,
    "id_str": "1409976027426611201",
    "text": "Today, we announced the release of two new reliability features for the Twitter API v2 streaming endpoints: redunda… ",
    "truncated": true,
    "entities": {
      "hashtags": [],
      "symbols": [],
      "user_mentions": [],
      "urls": [
        {
          "url": "",
          "expanded_url": "",
          "display_url": "twitter.com/i/web/status/1…",
          "indices": [
            117,
            140
          ]
        }
      ]

Note : Response is too big to print here. If you notice it starts with Array , i have added tree hirearchy img at the end of the post. please refer

When i am trying to run below code .

JsonPath js1 = new JsonPath(responseTweets);
        
        int len = js1.getInt("array.size()");
        System.out.println("Length of the Array " + len);
        
        System.out.println(js1.get("array[0].id"));
    
        
      for(int i=0; i<len; i++)
        {
            System.out.println(js1.get("array["+i+"].text"));
        }

Its showing Output:

Length of the Array 5
null
null
null
null
null
null

Can you please help me to underatand how to fetch the values from this JSON.

enter image description here

Upvotes: 0

Views: 164

Answers (1)

lucas-nguyen-17
lucas-nguyen-17

Reputation: 5917

I use JsonPath (com.jayway.jsonpath.JsonPath). It's so easy to compare with JsonPath(io.restassured.path.json.JsonPath).

List<String> list = JsonPath.read(res, "$[*].text");
System.out.println(list);

//["Today, we\u2019re launching new manage mutes into Twitter API v2.\n\n#TwitterAPI #v2 #EarlyAccess","Today, we announced the release of two new reliability features for the Twitter API v2 streaming endpoints: redunda\u2026 "]

pom.xml

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.6.0</version>
</dependency>

Upvotes: 2

Related Questions