Best answer

What format does JSON gets converted to in the input bar for custom code.

  • 16 November 2022
  • 4 replies
  • 20 views

Userlevel 1

Hello,

 

I am bringing in a json to a custom code step and it just isn’t converting to json to me. Not sure what to do anymore. I’ve defined this simple example to demonstrate:

You can see that I have “input” and “code” to be defined exactly the same. Yet the output for them are different format. I would love input to put output the same as “in_code”

 

I couldn’t find anything specific to Zapier that would help here.

Thanks!

icon

Best answer by Troy Tessalone 17 November 2022, 18:05

View original

4 replies

Userlevel 7
Badge +14

Hi @Arnoldas 

Good question.

Here are help articles for how to convert to JSON:

Python: https://www.w3schools.com/python/gloss_python_json_parse.asp

JavaScript: https://www.w3schools.com/js/js_json_parse.asp

 

 

Userlevel 1

Appreciate the answer! 

That’s definitely what I’ve tried right away (but it didn’t work in the zapier code runner) - so I wasn’t sure if this is the right approach.

Here I’ve added the “loads” variable to test this:

import json

input = input_data['x']
code = [{'title': 'Shipping', 'unit_price_amount': None, 'quantity': 0}]

loads = json.loads(input)

same = input == code


output = [{'input':input, 'in_code': code, 'load':loads}]

Here’s what I got:

Error:

Your code had an error! Traceback (most recent call last): File "<string>", line 13, in the_function File "/var/lang/lib/python3.7/json/__init__.py", line 348, in loads return _default_decoder.decode(s) File "/var/lang/lib/python3.7/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/var/lang/lib/python3.7/json/decoder.py", line 353, in raw_decode obj, end = self.scan_once(s, idx) JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 3 (char 2)

 

Userlevel 7
Badge +14

@Arnoldas

Give this JavaScript Code a try…

{'title': 'Shipping', 'unit_price_amount': None, 'quantity': 0}
let X = inputData.X; // get raw JSON as input
X = X.replace(/'/g,'"'); // replace all ' with "
X = X.replace(/None/g,'"None"'); // replace None with "None"
X = JSON.parse(X); // parse JSON

output = [{X}];

 

 

Userlevel 1

This did the trick! 

 

Thank you very much. Medal worthy support!

Reply