-1

I'm getting JSON data from a API but in that API I'm getting number without quote now I need to add quotes how can I added quotes with numbers?

Right now I'm getting:-

[{"name":"Hakim","age":32},{"name":"Girish","age":35}

I want to get:-

[{"name":"Hakim","age":"32"},{"name":"Girish","age":"35"}

Demo URL:

https://codesandbox.io/s/double-quote-issue-nkhkye?file=/src/App.js:0-555

My Code:-

import React from "react";
import "./styles.css";
const real_data = [
  {
    name: "Hakim",
    age: 32
  },
  {
    name: "Girish",
    age: 35
  }
];
const update_data = JSON.stringify(real_data);
console.log(update_data);
export default function App() {
  //  const [data, setData] = React.useState([]);
  // setData(real_data);
  return (
    <div className="App">
      {/* {data
        ? data.map((opt) => (
            <h2 key={opt.age} value={opt.age}>
              {opt.name}
            </h2>
          ))
        : ""} */}
    </div>
  );
}

Thanks for your efforts!

9
  • "Add quotes" where? For what purpose? It's not clear to me what you're asking or what problem you're observing. In general if you want to turn a number into a string then you can append .toString() to it.
    – David
    Oct 3, 2022 at 15:58
  • let me modify my question to explain what i want to do. Oct 3, 2022 at 15:59
  • 1
    console.log(real_data.map(d=> {d.age =d.age.toString(); return d}));
    – KcH
    Oct 3, 2022 at 16:07
  • 2
    You're better off fixing the data that's sent to you if you can but it's not clear why you want to add the quotes. Is something not working?
    – Andy
    Oct 3, 2022 at 16:07
  • 1
    @RohitVerma yeah, so it makes sense to fix the original data that way you don't have to do any transformations on the client before posting it back.
    – Andy
    Oct 3, 2022 at 17:13

1 Answer 1

2
const stringAges = real_data.map(e => ({...e, age: e.age + '' }));
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.