0

I am returning a List of JSON items back from the front end. It current looks like:

[
  "[{ "_id" : ObjectId("5cbcd80e0c5c9f1dfc8bf2f3"), "price" : "$1,
  399.00", "name" : "AlienwareGamingPCDesktopAuroraR7-8thGenIntelCorei7-8700,
  16GBDDR4Memory,
  2TBHardDrive+32GBIntelOptane,
  ?NVIDIAGeForceGTX10808GBGDDR5X,
  Windows1064bit\", "url" : "https: //www.amazon.com/gp/product/B076BHG74V/ref=s9_acsd_zwish_hd_bw_b2N0U_>c_x_w?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=merchandised-search-11&pf_rd_r=TW0CMS0QN07XT2BM838T&pf_rd_t=101&pf_rd_p=62f4ca32-9706-51c1-a1d1-9f7214564c34&pf_rd_i=565098\", "description" : "ThenewAlienwareAuroraisamid-towerdesktopwithaMicroATXmotherboardandisalsooursmallestdual-graphicscapabledesktopandhasmoretool-lessfeaturesthananypreviousAlienwaredesktopsuchas: tool-lessgraphics,
  expansioncards,
  hard>drives,
  andmemory.", "keywords" : ["computer"] }]"
]

When displaying the results with: console.log(results[0]), it displays the whole object. When doing console.log(results[0].name) or console.log(results[0].name) I get a undefined error. There is current only one object in the list, but for future queries there will be more. Any ideas on how to access individual elements?

What the code in the frontend looks like:

.then(response => {
            this.amazonItems = response.data;
            console.log(this.amazonItems[0]);

Query of MongoDB:

var result = collection.Find(filter).Sort(sort).ToList().ToJson();

Serialization:

List<string> returnResult = new List<string>();
returnResult.Add(_sm.SearchAmazonQuery(searchQuery, _db));
response.Content = new StringContent(JsonConvert.SerializeObject(returnResult), System.Text.Encoding.UTF8, "application/json");
response.StatusCode = HttpStatusCode.OK;
return response;

Console output: console

4
  • Too much JSON. Commented May 13, 2019 at 4:48
  • Please show screenshots of your console, copying its output as text isn't always clear. Also, how are you creating the request? Commented May 13, 2019 at 4:51
  • From your comment below, what is returnResult? I suspect it's a list / array / collection / whatever of already JSON-encoded strings which would be why you're getting this double-encoded response Commented May 13, 2019 at 5:08
  • @Phil I added a answer....provided more information. Commented May 13, 2019 at 5:09

3 Answers 3

1

Try parsing, I think it is in stringify mode.

.then(response => {
    this.amazonItems = response.data;
    console.log(JSON.parse(this.amazonItems[0]));
Sign up to request clarification or add additional context in comments.

2 Comments

FYI, it's JSON.parse (lowercase "p"). Also, I suspect OP's response is double JSON encoded, otherwise amazonItems[0] wouldn't work
I forgot to add, I serialize the list on the backend to JSON, so parsing wont work. response.Content = new StringContent(JsonConvert.SerializeObject(returnResult), System.Text.Encoding.UTF8, "application/json");
0

It looks like it’s stringified, try JSON.parse(results[0]), also that’s some weird syntax ObjectId(‘asdajdjsja’)

2 Comments

ObjectId indicates the response is a MongoDB doc entry
Correct, its coming from Mongo.
0

I don't really know much about your server-side code (guessing C#) but, assuming

_sm.SearchAmazonQuery(searchQuery, _db)

returns the result of

var result = collection.Find(filter).Sort(sort).ToList().ToJson();

which appears to already be a JSON string, I suspect you just want

response.Content = new StringContent(_sm.SearchAmazonQuery(searchQuery, _db), 
        System.Text.Encoding.UTF8, "application/json");

This means your response.data will be an array of objects, not an array of JSON strings.

.then(response => {
  this.amazonItems = response.data
  console.log(this.amazonItems[0])
    // 👉 {_id: "5cbcd80e0c5c9f1dfc8bf2f3", price: "$1,399.00", ...
})

4 Comments

You are correct in your assumptions. I tried your fix, the result of console.log(amazonItems) is now just the same as the result from the image. Doing console.log(amazonItems[0] results in a single ' [ ' and amazonItems.name is results in undefined.
That's odd as Axios should be decoding the response as JSON automatically
Not sure what else to do. Wouldn't be so bad if I could access the elements.
We'll need to see exactly what the response is. Use your browser's Network console. Check the response headers and please show exactly what the response body looks like

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.