Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 286925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:40:49+00:00 2026-05-12T05:40:49+00:00

I am fetching a .js file from a remote site that contains data I

  • 0

I am fetching a .js file from a remote site that contains data I want to process as JSON using the simplejson library on my Google App Engine site. The .js file looks like this:

var txns = [
    { apples: '100', oranges: '20', type: 'SELL'}, 
    { apples: '200', oranges: '10', type: 'BUY'}]

I have no control over the format of this file. What I did at first just to hack through it was to chop the "var txns = " bit off of the string and then do a series of .replace(old, new, [count]) on the string until it looked like standard JSON:

cleanJSON = malformedJSON.replace("'", '"').replace('apples:', '"apples":').replace('oranges:', '"oranges":').replace('type:', '"type":').replace('{', '{"transaction":{').replace('}', '}}')

So that it now looks like:

[{ "transaction" : { "apples": "100", "oranges": "20", "type": "SELL"} }, 
 { "transaction" : { "apples": "200", "oranges": "10", "type": "BUY"} }]

How would you tackle this formatting issue? Is there a known way (library, script) to format a JavaScript array into JSON notation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:40:49+00:00Added an answer on May 12, 2026 at 5:40 am

    It’s not too difficult to write your own little parsor for that using PyParsing.

    import json
    from pyparsing import *
    
    data = """var txns = [
       { apples: '100', oranges: '20', type: 'SELL'}, 
       { apples: '200', oranges: '10', type: 'BUY'}]"""
    
    
    def js_grammar():
        key = Word(alphas).setResultsName("key")
        value = QuotedString("'").setResultsName("value")
        pair = Group(key + Literal(":").suppress() + value)
        object_ = nestedExpr("{", "}", delimitedList(pair, ","))
        array = nestedExpr("[", "]", delimitedList(object_, ","))
        return array + StringEnd()
    
    JS_GRAMMAR = js_grammar()
    
    def parse(js):
        return JS_GRAMMAR.parseString(js[len("var txns = "):])[0]
    
    def to_dict(object_):
        return dict((p.key, p.value) for p in object_)
    
    result = [
        {"transaction": to_dict(object_)}
        for object_ in parse(data)]
    print json.dumps(result)
    

    This is going to print

    [{"transaction": {"type": "SELL", "apples": "100", "oranges": "20"}},
     {"transaction": {"type": "BUY", "apples": "200", "oranges": "10"}}]
    

    You can also add the assignment to the grammar itself. Given there are already off-the-shelf parsers for it, you should better use those.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a chrome extension that requires fetching an xml file from a secure
I want to create a pdf file from Binary data. I looked around and
We've been using information from a site for a while now (something that the
I am using google's appengine api from google.appengine.api import urlfetch to fetch a webpage.
I am fetching current data from another company's web feed. It is a simple
My application is fetching a data dynamically from MYSQL and displaying. The Problem is
My code fetches CSV data from a PHP page using httplib. When I open
I want to do xml parsing in j2me for fetching data (sms, mms, location)
I am fetching an array of floats from my database but the array I
I have a quick question about fetching results from a weakly typed cursor and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.