0

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,

1

2 Answers 2

1

I think you need data.datajson[0].id

Sign up to request clarification or add additional context in comments.

7 Comments

Hi @ehacinom , I get the following: TypeError: Can not read property '0' of undefined. What is happening?
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/…
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
what does console.log(data) log? what about console.log(typeof data)
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 ...
|
0

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 the datajson property is a string
  • so let's parse again the above object nestedData = JSON.parse(data.datajson)

2 Comments

I get: Unexpected token or in JSON at position 0
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 :)

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.