Inputs to json via python

Okay here is my story.

I’m working with the Dynamo Sandbox 2.17.1 and I’m working on a graph to create a new project via a web API.

I’m working with the DynaWeb package and the Data-Shapes package.

I’m running into a problem trying to convert (concatenate?) 3 inputs into some json for the web API.

The inputs are two strings and 1 integer.

the json format is:

{
“Name”: “string”,
“ProjectTemplateId”: 0,
“Number”: “string”,
}

If I hardcode the above in a code block I can get it to work, but I’m trying to be able to use some Data-Shapes pop-up to be able to input the string data (Name & Number) and pick the Project template from a list (also via Data-shapes pop-up).

I thought the best way to do this was via a python script, which is likely the problem as I have very little Python skillz :slight_smile:

I have got the 3 pop-ups working, and if I do a simple python script to check the outputs after selecting from the pop-ups the outputs are correct (2 strings 1 number)

showing correct outputs


python script that got the outputs above
DynamoSandbox_Q0yy7QgCUM

My attempt to convert (concatenate?) in python is below and gives an error
DynamoSandbox_XElSJcfx9F
Error
DynamoSandbox_MGXlBbDO8w

So I tried to improve my syntax with the python graph below, but no luck (and really not a big surprise
DynamoSandbox_1DYSDaIzJe

Which gave me this error
DynamoSandbox_YxSRy0scmf

I’m sure with a little more python research I can get this to work. So I will keep working on my python, but thought I would ask here if anyone has come across a similar issue, and perhaps a solution.

Thanks!

Hey,

I think triple quotation marks “”" are your friend here :slight_smile:

image

Hope that helps,

Mark

using a Backslash (necessary when the code writes on several lines without special character)

Update!

I tried this script and it worked without error (yeah!) but the inputs are in so list or array out but not the needed string or number (but this info in correct in the square brackets.

Script
DynamoSandbox_8gtzSeRF58
Output

DynamoSandbox_2bRXzmO27a

full output below

[
{“Name”: [“test 9”], “ProjectTemplateId”: [2156], “Number”: [“009”]}
]

Thanks, @Mark.Ackerley!

this gives me the correct output, but it doesn’t read the inputs, so the inputs are not considered so the final output is not as expected ;(

DynamoSandbox_YDghxU72I8

As per the input the name should be “test 9”
As per the input the project template should be 2156
As per the input the number should be “009”

Thanks @c.poupin this answer does take the inputs into account, but I can’t use the dictionary output type in the header (the json response that the API is looking for)

for info, you can also use the json library

an example


import clr
import sys
import System
from System.IO import *
from System.Net import *
import time
clr.AddReference('System.Web.Extensions')
from System.Web.Script.Serialization import JavaScriptSerializer
import json

request = WebRequest.Create("https://random-data-api.com/api/address/random_address")
with request.GetResponse() as response:	
	with response.GetResponseStream() as stream:
		with StreamReader(stream) as reader:
			data_json = reader.ReadToEnd()
			#html = html.encode("utf-8")
			#js = JavaScriptSerializer()
			#dataDict = js.DeserializeObject(data_json)
			parsed = json.loads(data_json)
			OUT = parsed
2 Likes

It is not pretty but I was able to extract the square brackets from the output and then use the new “clean” string to pass on to the web API and it worked :slightly_smiling_face:

I’m sure there is a cleaner way to do this, but for now, this works.

Thanks a TON to @c.poupin and @Mark.Ackerley for their input and help getting me over the finish line!!

P.S. I used this python script

DynamoSandbox_8gtzSeRF58

1 Like

I know that Python was in the title, and there isn’t any problem with this solution, but I wanted to point out that there are also nodes to create JSON and parse JSON in the standard library called Data.StringifyJSON and Data.ParseJSON respectively, which could save some headaches down the line.

These aren’t the newest nodes in the world, but they’ve bene around for a few years at least so you should have them in the 2.13+ build your screenshots are from.

3 Likes

Heya,

I imagined it would work like this…

Sorry if I wasn’t clear, glad you got it working! :slight_smile:

I expect Cyril and Jacobs solutions are better than mine :smiley:

Mark

1 Like