question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Converting JSONata to valid JSON

See original GitHub issue

Is there a way to convert JSONata string to valid JSON ? Assumption : JSONata string does not involve any function declarations.

Example :

    var script = `{
        "source": source,
        "data":
        {
            "name" : data.payload.Name.(FirstName & ' ' & LastName),    
            "alias": data.payload.Name.(Salutation & ' ' & FirstName),
            "active": data.payload.Status = 'New' ? true : false,
            "signature": "Have good day ," & data.payload.Name.FirstName & "!"  
        }       
    }`;

Is there a way to convert above to JSON like this :

{
  "source": "source",
  "data": {
    "name": "data.payload.Name.(FirstName %26 %27 %27 %26 LastName)",
    "alias": "data.payload.Name.(Salutation %26 %27 %27 %26 FirstName)",
    "active": "data.payload.Status = %27New%27 ? true : false",
    "signature": "%22Have good day ,%22 %26 data.payload.Name.FirstName %26 %22!%22"
  }
}

All special character has been encoded using escape() function in javascript.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
andrew-colemancommented, Sep 26, 2019

This feels like a Javascript discussion. I will close it here and let the discussion continue on stack overflow.

0reactions
UtsavChokshiCNUcommented, Sep 26, 2019

@jhorbulyk

var script = `{
    "source": source,
    "data":
    {
    	"name" : data.payload.Name.(FirstName & ' ' & LastName),    
      "alias": data.payload.Name.(Salutation & ' ' & FirstName),
      "active": data.payload.Status = 'New' ? true : false,
      "email": data.payload.Email,
      "signature": "Have good day ," & data.payload.Name.FirstName & "!"
     }       
}`;

var x = JSON.parse(JSON.stringify(script));
print(x);
print(x["data"]["name"]);

Gives error : TypeError: Cannot read property 'name' of undefined

Read more comments on GitHub >

github_iconTop Results From Across the Web

String functions · JSONata
Strings are unchanged; Functions are converted to an empty string; Numeric infinity and NaN ... If prettify is true, then "prettified" JSON is...
Read more >
Applying JSONata functions to transform your data - IBM
When editing a target field in a flow, you can add a transform function to ... If a JSONata expression is not valid,...
Read more >
JSONata Cheatsheet - Stedi Docs
All mappings expressions are based on JSONata language - this page showcases its most important features useful when creating a mapping.
Read more >
Convert String to JSON Objects in JavaScript with eval()
String data can be converted to JSON using the stringify() function or eval(), which accepts the JavaScript expression you will learn about ...
Read more >
10 Examples to Help You Learn JSONata - Blues Wireless
This guide's examples all take the same input JSON (shown below), and use JSONata to query or transform that input in interesting ways....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found