I need to convert docx.text.paragraph.Paragraph object to string in python - python-docx

I need to convert to string. Here is my code
single_para = doc.paragraphs[1]
str(single_para)

Use doc.paragraphs[1].text. This returns the text of the paragraph as a string.

Related

JSONPath: Get field starting with # in escaped json string

I want to get a nested field in a json string using JSONPath.
Take for example the following json:
{
"ID": "2ac464eb-352f-4e36-8b9f-950a24bb9586",
"PAYLOAD": "{\"#type\":\"Event\",\"id\":\"baf223c4-4264-415a-8de5-61c9c709c0d2\"}"
}
If I want to extract the #type field, I expect to do it like this
$.PAYLOAD.#type
But that doesn't seem to work..
Also tried this:
$.PAYLOAD['#type']
Do I need to use escape chars or something?
Posting my comment as an answer
"{\"#type\":\"Event\",\"id\":\"baf223c4-4264-415a-8de5-61c9c709c0d2\"}"
Isn't JSON, it's a string containing encoded JSON.
Since JsonPath can't decode such string, you'll have to use a language of your desire to decode the string.
Eg: Decoding JSON String in Java

Convert to string in Postman?

In Postman, I use {{$randomUUID}} as a parameter, but I need to pass it as string (text) in the request. However, there is no convert function in Postman and I could not found any proper way to convert this generated UUID to string. So, how can I convert it to string in Postman and pass it as string?
I also tried something like "menuId": {{"" + $randomUUID}},, but is does not work.
{
"menuId": "{{$randomUUID}}"
}
you just have to enclose it with double quotes

What format is this in?

What format is this request in?
"customer_id=5&products%5B0%5D%5Barticle_id%5D=4099&products%5B0%5D%5Bquantity%5D=1&products%5B0%5D%5Btotal_price%5D=0"
is there a way to automatically convert JSON or normal text into this format?
This request string appears to be encoded for a URL/URI.
You can use decodeURIComponent to see the original string. For example:
var s = "customer_id=5&products%5B0%5D%5Barticle_id%5D=4099&products%5B0%5D%5Bquantity%5D=1&products%5B0%5D%5Btotal_price%5D=0";
var original = decodeURIComponent(s);
This will yield the following:
"customer_id=5&products[0][article_id]=4099&products[0][quantity]=1&products[0][total_price]=0"
To convert to this format you can use the inverse function called encodeURIComponent on the original string.

Ruby parse string to json

So I have some json that looks like this, which I got after taking it out of some other json by doing response.body.to_json:
{\n \"access_token\": \"<some_access_token>\",\n \"token_type\": \"Bearer\",\n \"expires_in\": 3600,\n \"id_token\": \<some_token>\"\n}\n"
I want to pull out the access_token, so I do
to_return = {token: responseJson[:access_token]}
but this gives me a
TypeError: no implicit conversion of Symbol into Integer
Why? How do I get my access token out? Why are there random backslashes everywhere?
to_json doesn't parse JSON - it does the complete opposite: it turns a ruby object into a string containing the JSON representation of that object is.
It's not clear from your question what response.body is. It could be a string, or depending on your http library it might have already been parsed for you.
If the latter then
response.body["access_token"]
Will be your token, if the former then try
JSON.parse(response.body)["access_token"]
Use with double quotes when calling access_token. Like below:
to_return = {token: responseJson["access_token"]}
Or backslashes are escaped delimiters and make sure you first parse JSON.

JSON.parse file input differ from parsing string literal

Im using nodejs to parse some JSON files and insert them into mongodb,the JSON in these files have invalid JSON characters like \n,\" etc ..
The thing that i dont understand is that if i tried to parse like :
console.log(JSON.parse('{"foo":"bar\n"}'))
i get
undefined:1
{"foo":"bar
but if i tried to parse the input from the file (The file has the same string {"foo":"bar\n"})like:
new lazy(fs.createReadStream("info.json"))
.lines
.forEach(function(line){
var line = line.toString();
console.log(JSON.parse(line));
}
);
every thing works fine , i want to know if this fine and its ok to parse the files i have, or i should replace all invalid JSON characters before i parse the files ,
and why is there a difference between the two.
Thanks
If you can read "\n" if your text file, then it's not an end of line but the \ character followed by a n.
\n in a JavaScript string literal adds an end of line and they're forbidden in JSON strings.
See json.org :
To put an end of line in a JSON string, you must escape it, which means you must escape the \ in a JavaScript string so that there's "\n" in the string received by JSON.parse :
console.log(JSON.parse('{"foo":"bar\\n"}'))
This would produce an object whose foo property value would contain an end of line :