skip to Main Content

My JSON response values have single quote but I want double quote. I have already tried JSON.stringfy() and JSON.parse(), they both are not working.

Response:

[
  {
    title: 'Car',
    price: 2323,
  }
]

Expected Response:

[
  {
    title: "Car",
    price: 2323,

  }
]

Basically, I want to use that response in shopify graphql query.

mutation {

    productCreate(input: {
      id:"gid://shopify/Product/4725894742116"
      title: "This is a car",
        variants:[{
        title:"car",
        price: 12
        }]
    }) {
      product {
        id
      }
    }
  }

3

Answers


  1. You could apply: JSON.stringify (which converts a JS object to a JSON string), then JSON.parse (which parses a JSON string back to a JS object), e.g.

    let x = [{
      title: 'Car',
      price: 2323,
    }];
    x = JSON.parse(JSON.stringify(x));
    console.log(x);
    Login or Signup to reply.
  2. I don’t see, any problem using JSON.stringify you can either get the string directly and use it inside a query or if you need a javascript object, you can just parse it.

    JSON.Stringify

    JSON.parse

    Passing arguments in GraphQl

    const unwantedResponse = [{
      title: 'Car',
      price: 2323,
    }]
    
    const wantedResponse = JSON.stringify(unwantedResponse);
    const parsedResponse = JSON.parse(wantedResponse)
    
    console.log(wantedResponse);
    console.log(parsedResponse);
    Login or Signup to reply.
  3. You can use JSON.parse() method parses a JSON string
    and The JSON.stringify() method converts a JavaScript object or value to a JSON string.

    let obj =[
      {
        title: 'Car',
        price: 2323,
      }
    ];
    
    let result = JSON.parse(JSON.stringify(obj));
    
    
    
    console.log(result);
    

    the result is

    [
      {
        title: "Car",
        price: 2323,
    
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search