Skip to main content
Best answer

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

  • November 16, 2022
  • 4 replies
  • 237 views

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!

Best answer by Troy TessaloneBest answer by Troy Tessalone

@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}];

 

 

View original
Did this topic help you find an answer to your question?
This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

4 replies

Troy Tessalone
Forum|alt.badge.img+14
  • Zapier Expert
  • 30800 replies
  • November 17, 2022

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

 

 


  • Author
  • Beginner
  • 6 replies
  • November 17, 2022

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)

 


Troy Tessalone
Forum|alt.badge.img+14
  • Zapier Expert
  • 30800 replies
  • Answer
  • November 17, 2022

@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}];

 

 


  • Author
  • Beginner
  • 6 replies
  • November 17, 2022

This did the trick! 

 

Thank you very much. Medal worthy support!