2

I am using react to fetch Json data from an API, however, the API includes a key that has an object formatted as a String. See example below:

 user_category:"employee",
 user_info:"{"user_id":"55","user_age":"27","user_company":"tesla"}"

To access the user-category, I simply use a header with an accessor, and the value displays on the table just fine, however I am having difficulties accessing the user_info key String using its keys and values using something like this:

 {
     Header: "User Id",
     accessor: "user_info.user_id"
   },
   {
     Header: "User Age",
     accessor: "user_info.user_age"
   },
   {
     Header: "User Company",
     accessor: "user_info.user_company"
   }

1 Answer 1

3

It’s odd that the server is double-encoding the object to JSON like that (encoding the inner one and then encoding the whole thing).

Ideally you’d have the server-side fixed, because what they’re doing doesn’t make sense, as JSON supports nested objects just fine.

If you have to solve the problem on the client, you’d use JSON.parse to turn the string into an object.

const atts = {
  user_category: "employee",
  user_info: "{"user_id":"55","user_age":"27","user_company":"tesla"}"
};

const userInfo = JSON.parse(atts.user_info);
Sign up to request clarification or add additional context in comments.

2 Comments

I am nut sure I can wrap up the response in an atts constant because it is loaded directly from the server...
Right. So I’m not familiar with React, but you’ll probably need to make a function that takes in user_info (that’s in props or data or something?) and does this parsing.

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.