Home > Software design >  Failed to Parse JSON [closed]
Failed to Parse JSON [closed]

Time:09-21

I am getting failed to parse JSON on my JS code, and when I tried to print it on console and beautify it on online JSON beautifier. It gives me this error

Parse error on line 1:
...-06-05 11:21:48.0"} ","productSellCatego
-----------------------^
Expecting 'EOF', '}', ',', ']', got 'STRING'

JSON

[{"id":"1" ,"storeName":"MyStore" ,"uniqueStoreCode":"MytURz718" ,"storeDescription":"how about this store" ,"storeStatus":"0" ,"gstinNumber":"mFnbkYGU5BdmijB0ZSp9Xw==" ,"sellerPanNumber":"MNBVK8520U" ,"storeAccountNumber":"Lf4HJliBJzWXIcYUW2CD7Q==" ,"country":"India" ,"state":"Uttar" ,"city":"Ghaziabad" ,"address":"b-1/136 s-1" ,"storeAccountHolderName":"Ramesh Sharma" ,"bankAccountType":"0" ,"addedAt":"2021-08-12 09:40:37.0" ,"storeOwner":{"id":"2", "uniqueUserId":"sec7M3dkH", "firstName":"Ramesh", "lastName":"Sharma", "dateOfBirth":"1979-07-23", "email":"[email protected]", "userName":"seller", "status":"0", "role":"1", "country":"null", "state":"null", "city":"null", "zipCode":"null", "address":"null", "gender":"0", "phoneNumber":" 919811708085", "addedAt":"2021-06-05 11:21:48.0"} ","productSellCategory":{"productCatId":"6", "categoryName":"Clothing and Accessories"}" ,"addedAt":"2021-08-12 09:40:37.0" } , {"id":"2" ,"storeName":"MyStore" ,"uniqueStoreCode":"My1mQfYDF" ,"storeDescription":"how about this store" ,"storeStatus":"0" ,"gstinNumber":"h/GcxLRmRljFa0nPE7GtRg==" ,"sellerPanNumber":"MNBVK8520U" ,"storeAccountNumber":"PkEBwQB/eiLktZBe8tDGPw==" ,"country":"India" ,"state":"Uttar" ,"city":"Ghaziabad" ,"address":"b-1/136 s-1" ,"storeAccountHolderName":"Ramesh Sharma" ,"bankAccountType":"0" ,"addedAt":"2021-08-12 09:43:02.0" ,"storeOwner":{"id":"2", "uniqueUserId":"sec7M3dkH", "firstName":"Ramesh", "lastName":"Sharma", "dateOfBirth":"1979-07-23", "email":"[email protected]", "userName":"seller", "status":"0", "role":"1", "country":"null", "state":"null", "city":"null", "zipCode":"null", "address":"null", "gender":"0", "phoneNumber":" 919811708085", "addedAt":"2021-06-05 11:21:48.0"} ","productSellCategory":"{"productCatId":"6", "categoryName":"Clothing and Accessories"}","addedAt":"2021-08-12 09:43:02.0" }    , {"id":"3" ,"storeName":"Sharma Ji Store" ,"uniqueStoreCode":"Shlv1BXrS" ,"storeDescription":"This is a beauty products store" ,"storeStatus":"0" ,"gstinNumber":"njauEy1GZ qEkLt02av/Fw==" ,"sellerPanNumber":"MNBVK8520U" ,"storeAccountNumber":"oc6ZgAfuufHkOt0fyO/lNQ==" ,"country":"India" ,"state":"Delhi" ,"city":"East" ,"address":"gali no.16 adarsh mohhala" ,"storeAccountHolderName":"Ramesh Sharma" ,"bankAccountType":"0" ,"addedAt":"2021-08-12 09:45:58.0" ,"storeOwner":{"id":"2", "uniqueUserId":"sec7M3dkH", "firstName":"Ramesh", "lastName":"Sharma", "dateOfBirth":"1979-07-23", "email":"sharmaascent6@gmail.com", "userName":"seller", "status":"0", "role":"1", "country":"null", "state":"null", "city":"null", "zipCode":"null", "address":"null", "gender":"0", "phoneNumber":" 919811708085", "addedAt":"2021-06-05 11:21:48.0"} ","productSellCategory":{"productCatId":"3", "categoryName":"Beauty"} ","addedAt":"2021-08-12 09:45:58.0" }  ]

My Functions to print strings are

storeOwner

@Override
    public String toString() {
        return "{\"id\":\""   id   "\", \"uniqueUserId\":\""   uniqueUserId   "\", \"firstName\":\""   firstName
                  "\", \"lastName\":\""   lastName   "\", \"dateOfBirth\":\""   dateOfBirth   "\", \"email\":\""   email
                  "\", \"userName\":\""   userName   "\", \"status\":\""   status   "\", \"role\":\""   role
                  "\", \"country\":\""   country   "\", \"state\":\""   state   "\", \"city\":\""   city
                  "\", \"zipCode\":\""   zipCode   "\", \"address\":\""   address   "\", \"gender\":\""   gender
                  "\", \"phoneNumber\":\""   phoneNumber   "\", \"addedAt\":\""   addedAt   "\"}";
    }

stores

@Override
    public String toString() {
        return "{\""  (id != null ? "id\":\""   id   "\" " : "")
                      (storeName != null ? ",\"storeName\":\""   storeName   "\" " : "")
                      (uniqueStoreCode != null ? ",\"uniqueStoreCode\":\""   uniqueStoreCode   "\" " : "")
                      (storeDescription != null ? ",\"storeDescription\":\""   storeDescription   "\" " : "")
                      (storeStatus != null ? ",\"storeStatus\":\""   storeStatus   "\" " : "")
                      (gstinNumber != null ? ",\"gstinNumber\":\""   gstinNumber   "\" " : "")
                      (sellerPanNumber != null ? ",\"sellerPanNumber\":\""   sellerPanNumber   "\" " : "")
                      (storeAccountNumber != null ? ",\"storeAccountNumber\":\""   storeAccountNumber   "\" " : "")
                      (country != null ? ",\"country\":\""   country   "\" " : "")
                      (state != null ? ",\"state\":\""   state   "\" " : "")
                      (city != null ? ",\"city\":\""   city   "\" " : "")
                      (address != null ? ",\"address\":\""   address   "\" " : "")
                      (storeAccountHolderName != null ? ",\"storeAccountHolderName\":\""   storeAccountHolderName   "\" " : "")
                      (bankAccountType != null ? ",\"bankAccountType\":\""   bankAccountType   "\" " : "")
                      (addedAt != null ? ",\"addedAt\":\""   addedAt   "\" " : "")
                       (Hibernate.isInitialized(storeOwner) && storeOwner != null ? ",\"storeOwner\":"   storeOwner   " \"" : "")
                       (Hibernate.isInitialized(productSellCategory) && productSellCategory != null ? ",\"productSellCategory\":"   productSellCategory   " \""  : "")
//                   (Hibernate.isInitialized(deliveryStatusSet) && deliveryStatusSet != null ? ", \"deliveryStatusSet\":"   deliveryStatusSet : "")
                      (addedAt != null ? ",\"addedAt\":\""   addedAt   "\" " : "")
                       "}\t";
    }

Product category

@Override
    public String toString() {
        return "{\"productCatId\":\""   productCatId   "\", \"categoryName\":\""   categoryName   "\"}";

//          return "{\""  (productCatId != null ? "productCatId\":\""   productCatId   "\" " : "")
//                    (categoryName != null ? ",\"categoryName\":\""   categoryName   "\" " : "")
//                     "}\t";
    
    }

CodePudding user response:

This is a very error prone way to create JSON.

Instead, create a POJO and use Gson or Jackson to convert it to a JSON string:

import com.google.gson.Gson;

public class SO69240970 {
    static class MyData {
        String a;
        String b;
        int c;
        public MyData(String a, String b, int c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }
    }

    public static void main(String[] args) {
        MyData m = new MyData("A value", "B \"value\"", 102);
        System.out.println(new Gson().toJson(m));
    }
}

Output: {"a":"A value","b":"B \"value\"","c":102}

Maven dependency:

    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.8</version>
    </dependency>

CodePudding user response:

In your json object is placed with quotes, removing those quotes should make it valid json e.g change ","productSellCategory":"{ to , "productSellCategory": {

your json after removing quotes is

[
    {
        "id": "1",
        "storeName": "MyStore",
        "uniqueStoreCode": "MytURz718",
        "storeDescription": "how about this store",
        "storeStatus": "0",
        "gstinNumber": "mFnbkYGU5BdmijB0ZSp9Xw==",
        "sellerPanNumber": "MNBVK8520U",
        "storeAccountNumber": "Lf4HJliBJzWXIcYUW2CD7Q==",
        "country": "India",
        "state": "Uttar",
        "city": "Ghaziabad",
        "address": "b-1/136 s-1",
        "storeAccountHolderName": "Ramesh Sharma",
        "bankAccountType": "0",
        "addedAt": "2021-08-12 09:40:37.0",
        "storeOwner": {
            "id": "2",
            "uniqueUserId": "sec7M3dkH",
            "firstName": "Ramesh",
            "lastName": "Sharma",
            "dateOfBirth": "1979-07-23",
            "email": "[email protected]",
            "userName": "seller",
            "status": "0",
            "role": "1",
            "country": "null",
            "state": "null",
            "city": "null",
            "zipCode": "null",
            "address": "null",
            "gender": "0",
            "phoneNumber": " 919811708085",
            "addedAt": "2021-06-05 11:21:48.0"
        },
        "productSellCategory": {
            "productCatId": "6",
            "categoryName": "Clothing and Accessories"
        },
        "addedAt": "2021-08-12 09:40:37.0"
    },
    {
        "id": "2",
        "storeName": "MyStore",
        "uniqueStoreCode": "My1mQfYDF",
        "storeDescription": "how about this store",
        "storeStatus": "0",
        "gstinNumber": "h/GcxLRmRljFa0nPE7GtRg==",
        "sellerPanNumber": "MNBVK8520U",
        "storeAccountNumber": "PkEBwQB/eiLktZBe8tDGPw==",
        "country": "India",
        "state": "Uttar",
        "city": "Ghaziabad",
        "address": "b-1/136 s-1",
        "storeAccountHolderName": "Ramesh Sharma",
        "bankAccountType": "0",
        "addedAt": "2021-08-12 09:43:02.0",
        "storeOwner": {
            "id": "2",
            "uniqueUserId": "sec7M3dkH",
            "firstName": "Ramesh",
            "lastName": "Sharma",
            "dateOfBirth": "1979-07-23",
            "email": "[email protected]",
            "userName": "seller",
            "status": "0",
            "role": "1",
            "country": "null",
            "state": "null",
            "city": "null",
            "zipCode": "null",
            "address": "null",
            "gender": "0",
            "phoneNumber": " 919811708085",
            "addedAt": "2021-06-05 11:21:48.0"
        },
        "productSellCategory": {
            "productCatId": "6",
            "categoryName": "Clothing and Accessories"
        },
        "addedAt": "2021-08-12 09: 43: 02.0"
    },
    {
        "id": "3",
        "storeName": "Sharma Ji Store",
        "uniqueStoreCode": "Shlv1BXrS",
        "storeDescription": "This is a beauty products store",
        "storeStatus": "0",
        "gstinNumber": "njauEy1GZ qEkLt02av/Fw==",
        "sellerPanNumber": "MNBVK8520U",
        "storeAccountNumber": "oc6ZgAfuufHkOt0fyO/lNQ==",
        "country": "India",
        "state": "Delhi",
        "city": "East",
        "address": "gali no.16 adarsh mohhala",
        "storeAccountHolderName": "Ramesh Sharma",
        "bankAccountType": "0",
        "addedAt": "2021-08-12 09: 45: 58.0",
        "storeOwner": {
            "id": "2",
            "uniqueUserId": "sec7M3dkH",
            "firstName": "Ramesh",
            "lastName": "Sharma",
            "dateOfBirth": "1979-07-23",
            "email": "[email protected]",
            "userName": "seller",
            "status": "0",
            "role": "1",
            "country": "null",
            "state": "null",
            "city": "null",
            "zipCode": "null",
            "address": "null",
            "gender": "0",
            "phoneNumber": " 919811708085",
            "addedAt": "2021-06-05 11: 21: 48.0"
        },
        "productSellCategory": {
            "productCatId": "3",
            "categoryName": "Beauty"
        },
        "addedAt": "2021-08-12 09: 45: 58.0"
    }
]

CodePudding user response:

this is what worked for me instead of

  • (Hibernate.isInitialized(storeOwner) && storeOwner != null ? ","storeOwner":" storeOwner " "" : "") (Hibernate.isInitialized(productSellCategory) && productSellCategory != null ? ","productSellCategory":" productSellCategory " "" : "")

I used

  • (Hibernate.isInitialized(storeOwner) && storeOwner != null ? ","storeOwner":" storeOwner :"" ) (Hibernate.isInitialized(productSellCategory) && productSellCategory != null ? ","productSellCategory":" productSellCategory : "")
  • Related