0

In react native in the following JSON I need to get an array of string ["abc description", "def description", "ghi description" and so on] for value of DataHolder. There could be n number of dictionary in DataHolder

{
  "StatusCode": 200,
  "Description": "Description",
  "Message": "Success",
  "Response": {
    "Data": {
      "DataHolder": [
        {
          "abc": "abc description"
        },
        {
          "def": "def description"
        },
        {
          "ghi": "ghi description"
        }
      ]
    }
  }
}

I am new in react-native any help will be appreciated.

0

2 Answers 2

2

Sounds like you would just like to restructure the data from an API response. Here is one solution:

const data = {
    ...
    "Response": {
        "Data": {
            "DataHolder": [
                {
                    "abc": "abc description"
                },
                {
                    "def": "def description"
                },
                {
                    "ghi": "ghi description"
                }
            ]
        }
    }
};
const descriptions = data.Response.Data.DataHolder.map(item => Object.values(item)[0]);

// ["abc description", "def description", "ghi description"]
Sign up to request clarification or add additional context in comments.

Comments

0

One solution would be using JSON library

 const tempJSON = {
      "StatusCode": 200,
      "Description": "Description",
      "Message": "Success",
      "Response": {
        "Data": {
          "DataHolder": [
            {
              "abc": "abc description"
            },
            {
              "def": "def description"
            },
            {
              "ghi": "ghi description"
            }
          ]
        }
      }
    }

    let resultJSON = JSON.stringify(tempJSON)
    resultJSON = JSON.parse(resultJSON)

    console.log(resultJSON.Response.Data.DataHolder)

log result

(3) [{…}, {…}, {…}]
0: {abc: "abc description"}
1: {def: "def description"}
2: {ghi: "ghi description"}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.