0

I have a Python 2.7 script that looks like this:

import requests

    url = "https://example.net/rest/v1/m/4513452615415"

    querystring = {"client":"wzas"}

    payload = "{\r\n              \"sss\" : \""+msse+"\",\r\n\"VisitorId\":\""+mcid+"\",\r\n              \"thirdPartyId\": \""+tracking_id+"\",\r\n              \"contentAsJson\": \"true\",\r\n                             \"mbssParameters\": \r\n                             {        \r\n                             \"mboxMCGLH\": \"6\"     \r\n                             }\r\n}\r\n"
    headers = {
        'content-type': "application/json",
        'cache-control': "no-cache",
        'postman-token': "289f645d-1543-e6df-87fb-1cef88f110c5"
        }

    response = requests.request("POST", url, data=payload, headers=headers, params=querystring)

    return (response.text)

The output is sent to a CSV file and looks like this:

{"thirdPartyId":"559yZDIIs3XvFpLPhvjexK7/jYlT7ZwJXBpNc/ZS4A1saWdodG5pbmdzZWVkcw==","marketingId":"89137879111811717593914206190290951066814","edgeHost":"mrdge21.example.net","content":{"tnsVal":"931113:4:0","contentName":"examplecontent","revenue":14},"sessionId":"4513452615415"}

How would I only output the contentName and tnsVal ?

Thanks in advance Nick

1
  • HINT: Try response.json to get a dict Commented Nov 14, 2017 at 10:45

2 Answers 2

3

Since it's pretty clear you're getting a JSON back, try the following line:

import json
response_json = json.loads(response.text)
print(response_json['content']['contentName'], response_json['content']['tnsVal'])
Sign up to request clarification or add additional context in comments.

Comments

2

You can try:

>>> import json
>>> data = json.loads(response.text)
>>> data["content"]["tnsVal"]
'931113:4:0'
>>> data["content"]["contentName"]
'examplecontent'

Comments

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.