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

  • Home
  • SEARCH
  • 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 7831521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:46:10+00:00 2026-06-02T11:46:10+00:00

Say I have some JSON like the following: { items: { item: [ {

  • 0

Say I have some JSON like the following:

{
"items":
    {
        "item":
            [
                {
                    "id": "0001",
                    "type": "donut",
                    "name": "Cake",
                    "ppu": 0.55,
                    "batters":
                        {
                            "batter":
                                [
                                    { "id": "1001", "type": "Regular" },
                                    { "id": "1002", "type": "Chocolate" },
                                    { "id": "1003", "type": "Blueberry" },
                                    { "id": "1004", "type": "Devil's Food" }
                                ]
                        },
                    "topping":
                        [
                            { "id": "5001", "type": "None" },
                            { "id": "5002", "type": "Glazed" },
                            { "id": "5005", "type": "Sugar" },
                            { "id": "5007", "type": "Powdered Sugar" },
                            { "id": "5006", "type": "Chocolate with Sprinkles" },
                            { "id": "5003", "type": "Chocolate" },
                            { "id": "5004", "type": "Maple" }
                        ]
                },

                ...

            ]
    }
}

I’d like a function to return a list of tab delimited data (where -> is a tab). Something like this:

items.item.length -> 1
items.item[0].id -> 0001
items.item[0].type -> donut
items.item[0].name -> Cake
items.item[0].ppu -> 0.55
items.item[0].batters.batter.length -> 4
items.item[0].batters.batter[0].id -> 1001
items.item[0].batters.batter[0].type -> Regular
items.item[0].batters.batter[1].id -> 1002
items.item[0].batters.batter[1].type -> Chocolate
items.item[0].batters.batter[2].id -> 1003
items.item[0].batters.batter[2].type -> Blueberry
items.item[0].batters.batter[3].id -> 1004
items.item[0].batters.batter[3].type -> Devil's Food
items.item[0].topping.length -> 7
items.item[0].topping[0].id -> 5001
items.item[0].topping[0].type -> None
items.item[0].topping[0].id -> 5002
items.item[0].topping[0].type -> Glazed

...

I’m thinking of something like

function json2txt(obj) {
var txt = '';
    for (var key in obj) {
       if (obj.hasOwnProperty(key)) {
          if ("object" == typeof(obj[key])) {
             json2txt(obj[key]);
          } else txt += obj + '\t' + obj[key] + '\r';
       }
    }
}

“Oops! Your edit couldn’t be submitted because:

Your post does not have much context to explain the code sections; please explain your scenario more clearly.”

This is pretty frustrating too.

  • 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-06-02T11:46:12+00:00Added an answer on June 2, 2026 at 11:46 am

    You’re on the right track with the recursive function. You’ll need to add an argument to that function, though—it needs to know the path to the current point in the object.

    Also, use \n, not \r.

    var inputObject = {
        items: { 
            foo: [ 'apples', 'oranges', 'peaches' ],
            bar: 'baz',
            spam: 'eggs'
         }
    };
    function json2txt(obj, path)
    {
        var txt = '';
        for (var key in obj)
        {
            if (obj.hasOwnProperty(key))
            {
                if ('object' == typeof(obj[key]))
                {
                    txt += json2txt(obj[key], path + (path ? '.' : '') + key);
                } 
                else
                {
                    txt += path + '.' + key + '\t' + obj[key] + '\n';
                }
            }
        }
        return txt;
    }
    json2txt(inputObject, '');
    

    Fun problem!

    For your example data, my code gives:

    items.item.0.id 0001
    items.item.0.type   donut
    items.item.0.name   Cake
    items.item.0.ppu    0.55
    items.item.0.batters.batter.0.id    1001
    items.item.0.batters.batter.0.type  Regular
    items.item.0.batters.batter.1.id    1002
    items.item.0.batters.batter.1.type  Chocolate
    items.item.0.batters.batter.2.id    1003
    items.item.0.batters.batter.2.type  Blueberry
    items.item.0.batters.batter.3.id    1004
    items.item.0.batters.batter.3.type  Devil's Food
    items.item.0.topping.0.id   5001
    items.item.0.topping.0.type None
    items.item.0.topping.1.id   5002
    items.item.0.topping.1.type Glazed
    items.item.0.topping.2.id   5005
    items.item.0.topping.2.type Sugar
    items.item.0.topping.3.id   5007
    items.item.0.topping.3.type Powdered Sugar
    items.item.0.topping.4.id   5006
    items.item.0.topping.4.type Chocolate with Sprinkles
    items.item.0.topping.5.id   5003
    items.item.0.topping.5.type Chocolate
    items.item.0.topping.6.id   5004
    items.item.0.topping.6.type Maple
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have some json like this in mongo: {n:5} and a java
Let's say I have some code (using CherryPy) that looks like this: import cherrypy
Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) {
Can I store an array such as [1,2,3,4] or some JSON like {name:John Johnson,street:Oslo,
I have some JSON that looks like: groups: [ group_id: 8, group_name: Building, group_color:
Say I have some code like this function Chart(start, end, controller, method, chart) {
I have some JSON that is sent to my webservice that looks something like
Say I have some 10 categories that I need to reference in a web
Let say i have some words AB, AAB, AA. AB is not a prefix
Lets say we have some basic AR model. class User < ActiveRecord::Base attr_accessible :firstname,

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.