I'm trying to send a dict from python:
def toJsonCustom(self):
ajson = []
ajson.append({
'id': self.id,
'data': self.data
})
return json.dumps(
ajson, default=lambda obj: obj.__dict__, separators=(',', ':'),indent=4,
)
async def curl(url,data):
async with aiohttp.ClientSession() as session:
async with session.post(url, json={'datajson':data}) as response:
print(repr(response))
chunk = await response.content.read()
print('Downloaded: %s' % len(chunk))
#output JSON:
"{"datajson": "[\n {\n \"id\":\"test\",\n \"data\":\"test\"\n }\n]"}"
When trying to convert the output of this json into an object from javascript I get undefinied.
const data = JSON.parse(get(props, 'websocket.data', null)) || get(state, 'websocket.data') || 'No data';
console.log('Object json: ', (data));
# output: {"datajson": "[ n { n "id ": "test " n "data ": "test " n } n]"}
console.log('Object json: ', (data.id));
# output:undefined
How can I transform the json into a javascript object in a correct way?
Thank you,
-
I'm not very familiar with how python logs things but what you have under #output JSON is not json. Also: How to create a Minimal, Complete, and Verifiable exampleJabberwocky– Jabberwocky2019年05月03日 15:56:48 +00:00Commented May 3, 2019 at 15:56
2 Answers 2
I think you need data.datajson[0].id
answered May 3, 2019 at 15:52
ehacinom
9,0148 gold badges50 silver badges67 bronze badges
Sign up to request clarification or add additional context in comments.
7 Comments
Francisco
Hi @ehacinom , I get the following: TypeError: Can not read property '0' of undefined. What is happening?
ehacinom
when debugging, try do parts at a time. So, first logging out
console.log(data) is a good idea. next try console.log('datajson', data.datajson) (you don't need the parenthesis around the data). if that's undefined, you need to figure out the typeof your objects. Try typeof data, typeof data.datajson: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Francisco
console.log ('datajson', data.datajson) returns undefined. And when the json parses again, as @vassiliskrikonis says, I get: Unexpected token or in JSON at position 0
ehacinom
what does
console.log(data) log? what about console.log(typeof data)Francisco
console.log (data): {"datajson": "[\ n {\ n \" id \ ": \" test \ ", \ n \" value \ ": \" test \ "\ n} \ n]"} console.log (typeof data): string console.log ('datajson', data.datajson): datajson undefined . It seems that the JSON is well built. I have checked the data output from jsonformatter.curiousconcept.com I don't understand why I get undefined ...
|
It seems that you need to parse the JSON string twice to get the object you need:
- the first parse gives you the object
{ "datajson": "[{..." }, notice that the value of thedatajsonproperty is a string - so let's parse again the above object
nestedData = JSON.parse(data.datajson)
answered May 3, 2019 at 16:08
vassiliskrikonis
5962 silver badges10 bronze badges
2 Comments
Francisco
I get: Unexpected token or in JSON at position 0
vassiliskrikonis
As I saw in a comment above, if
data.datajson returns undefined then the issue is something else entirely. Try making a reproducible script as Jabberwocky commented you so we can help you :)lang-js